1

Hi basic question I'm struggling with

var objs = [ {name:'obj_1', evidence:[2] }, {name:'obj_2', evidence[1] } ]

I'd like to return an array of all 3 evidence objects

What I have so far is

_.filter(objs, function(obj) { 
  if (obj.evidence.length > 0) {
    _.each(obj, function(x) { return x }) 
  }
})

1 Answer 1

2

You can use _.pluck to reduce the array of objects to an array of a given property:

_.pluck(objs, 'evidence');

[ [2], [1] ]

If you wanted to flatten out the array, you could use "flatten":

_.flatten(_.pluck(objs, 'evidence'));

[2, 1]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.