我有一个像这样的复杂嵌套对象数组,我必须返回一个基于 attributeScore > 90
的新数组(过滤器)。我将如何使用 javascript .filter 或 lodash _.find() 或 _.some 函数来完成此操作?
trucks:[
{wheels:[
{name:"xyz",
mechanics: [
{engine:'50cc',
attributeScore:100},
{....} ,{...}
]
},
{name:"gcd",
mechanics: [
{engine:'80cc',
attributeScore:90},
{....} ,{...}
]
},
,{...}
]}
,{...}
]
我尝试过这样的过滤器
const fil = trucks.filter(function(item) {
return item.wheels.some(function(tag) {
return tag.mechanics.some(function(ques){
return ques.attributeScore <= 25;
});
});
});
但它返回一个空数组。我预期的返回数组类型应该是
trucks:[
{wheels:[
{name:"xyz",
mechanics: [
{engine:'50cc',
attributeScore:100},
{....} ,{...}
]
},
,{...}
]}
]
感谢任何帮助!!
最佳答案
现在就试试这个,
var fill = trucks.map(function(item) {
return item.wheels.map(function(tag) {
return tag.mechanics.filter(function(ques){
return ques.attributeScore == 100;
});
});
});
关于javascript - lodash 深度嵌套数组过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49402140/