Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lodash where function and getting nested array elements

I have the following type of array of objects, which I saved as image for you all so its easier to see:

enter image description here

Now what I want from this massive array is all the objects who's investments status is complete.

using the _.where, I tried to do (data being the giant array you see in the image):

var something =  _.where(data, function(item){ 
    return item.investments[0].statues === "complete" 
});

But um nothing seems to happen ... What am I doing wrong? I want objects out of the array who's investments status is complete.

ideas?

like image 415
TheWebs Avatar asked Dec 06 '22 19:12

TheWebs


1 Answers

I'd try out the filter method:

https://lodash.com/docs#filter

Iterates over elements of collection, returning an array of all elements predicate returns truthy for.

var results = _.filter(data, function(item){
  return item.investments.data[0].status === "complete";
});

And as BG101 mentioned: you didn't reference the data property of the investments object, so you were off a level.

like image 184
Alex Mcp Avatar answered Dec 30 '22 07:12

Alex Mcp