最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - lodash where function and getting nested array elements - Stack Overflow

programmeradmin3浏览0评论

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

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

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 === "plete" 
});

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

ideas?

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

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

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 === "plete" 
});

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

ideas?

Share Improve this question edited Nov 3, 2015 at 23:30 TheWebs asked Nov 3, 2015 at 23:27 TheWebsTheWebs 12.9k32 gold badges114 silver badges220 bronze badges 2
  • Unless it's a typo, you have ._where instead of _.where. status is also misspelled. If that's really in your code that will cause the failure for sure – Alex Mcp Commented Nov 3, 2015 at 23:28
  • Fixed he spelling mistake sorry I didn't copy and paste the code like I should have >_> – TheWebs Commented Nov 3, 2015 at 23:30
Add a ment  | 

2 Answers 2

Reset to default 9

I'd try out the filter method:

https://lodash./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 === "plete";
});

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

try:-

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

you have a typo in status and investments is an object with a property data

发布评论

评论列表(0)

  1. 暂无评论