0

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')] );
}

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

11 Comments

when i do console.log(filteredOutput ), i am getting empty arrays. [] [] []
What does your output variable look like?
msg = JSON.parse(packet.utf8Data).x and then using msg.output
so then, did you filter msg.output? the code is working for me - I'm getting only aaron, which is the only object meeting your criteria.
@AMB I've edited the code to account for your edit, which asks for the filter to pick elements where is_indb === false instead of true.
|
2

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)

4 Comments

This does not answer OP's question as it does not filter by is_indb.
its by far the simplest code. but there are some empty [] are showing in output, i guess some objects dont even have a single object which satisfies our condition.
it IS simpler, however it is also potentially error-prone in case you decide to change things later, (/^n/.test(null) === true).
@AMB That is probably because name and/or value are not set you could add that to the filter. nadavvadan, agree, solve a problem with regex, now you have two problems :)
0

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.

2 Comments

if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context); TypeError: [object Array] is not a function
I have used lodash for it var _ = require('lodash')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.