简体   繁体   中英

nodejs how can i use multidimensional array and push values in it?

i am trying to create a new multidimensional array from the data i am getting from 3rd part API.

"output":[
  {
     "is_indb":false,
     "name":"adam",
     "tokens":29
  },
  {
     "is_indb":true,
     "name":"aaron",
     "tokens":2,
  },
  {
     "is_indb":false,
     "name":"adam",
     "tokens":3,
  },
  {
     "is_indb":false,
     "name":"axel",
     "tokens":5,
  },
  {
     "is_indb":false,
     "name":"andy",
     "tokens":5,
  },
  {
     "is_indb":false,
     "name":"bob",
     "tokens":5,
  },
  {
     "is_indb":false,
     "name":"aldo",
     "tokens":5,
  },
  {
     "is_indb":false,
     "name":"julia",
     "tokens":5,
  }
]

i would like to create a new array and fill it with data from response. but i would like to do some pre checks like

take only those whose, is_indb = false take only those whose, name starts with a

so the final array will be, all those whosse is_indb = true and name starts with a

var newaray = [[adam,29],[adam,3],[axel,5],[andy,5],[aldo,5]];

so far i have tried using _pluck and getting some weird outputs. i am able to get sible elements using _pluck but cant get multiple items.

i can think of logic like this

var newaray = [];

if( (_pluck(msg.output,'is_indb') == false  && ((_pluck(msg.output,'name').substring(0, 1) == "a")){
    newaray.push( [ _.pluck(msg.output, 'name') , _.pluck(msg.output, 'token')] );
}

Use filter and map :

var filteredOutput = output
    .filter(function(elem) {
        // The return statement should return true,
        // if you want the element to pass into the new array.
        return elem.is_indb === false && typeof elem.name === "string" && elem.name.indexOf('a') === 0;
    })
    .map(function(elem) {
        return [elem.name, elem.tokens];
    });

or with ES6:

let filteredOutput = output
    .filter(elem => elem.is_indb === false && typeof elem.name === "string" && elem.name.indexOf('a') === 0)
    .map(elem => [elem.name, elem.tokens])

with ES6 and using regex (inspired by Peter Grainger 's answer, but also case insensitive):

let filteredOutput = output
        .filter(elem => elem.is_indb === false && /^a/i.test(elem.name))
        .map(elem => [elem.name, elem.tokens])

and by the way, what you posted is an array of objects, not a multidimensional array, which is an array of arrays.

You could use a filter then a map?

 const output = [ { "is_indb":false, "name":"adam", "tokens":29 }, { "is_indb":true, "name":"aaron", "tokens":2, }, { "is_indb":false, "name":"adam", "tokens":3, }, { "is_indb":false, "name":"axel", "tokens":5, }, { "is_indb":false, "name":"andy", "tokens":5, }, { "is_indb":false, "name":"bob", "tokens":5, }, { "is_indb":false, "name":"aldo", "tokens":5, }, { "is_indb":false, "name":"julia", "tokens":5, } ] const transform = output.filter(value => /^a/.test(value.name) && !value.is_indb) .map(value => [value.name, value.tokens]) console.log(transform) 

You can use _.filter and get the output in this form

op = [{ obj1 } ,{obj2}];

but as you want to remove some keys also then you can use _.pick

var op = _.filter(ip , function(obj){
    if(obj.is_indb == false && obj.name[0] == 'a'){
        return true;
    }
    else{
        return false;
    }
})
//so now you have all the entries filtered out
var opAltered = _.pick(op,['name','tokens']);

//you will get this result

/*
    opAltered = [
        {
            name : <something>,
            tokens : <something>
        },{
            ...
        }
    ]
*/

or If you want array you can use this

opAltered = _.map(op,['name','tokens'];

I have used more code to make you understand properly you can reduce it once you understand Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM