Javascript lodash深嵌套阵列滤波器
我有这样一个复杂的嵌套对象数组,我必须根据Javascript lodash深嵌套阵列滤波器,javascript,arrays,underscore.js,lodash,Javascript,Arrays,Underscore.js,Lodash,我有这样一个复杂的嵌套对象数组,我必须根据attributeScore>90返回一个新数组(过滤器)。我将如何使用javascript.filter或lodash u.find()或u.some函数实现这一点 trucks:[ {wheels:[ {name:"xyz", mechanics: [ {engine:'50cc', attributeS
attributeScore>90
返回一个新数组(过滤器)。我将如何使用javascript.filter或lodash u.find()或u.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;
});
});
});
谢谢你的帮助 现在试试这个
var fill = trucks.map(function(item) {
return item.wheels.map(function(tag) {
return tag.mechanics.filter(function(ques){
return ques.attributeScore == 100;
});
});
});
现在试试这个
var fill = trucks.map(function(item) {
return item.wheels.map(function(tag) {
return tag.mechanics.filter(function(ques){
return ques.attributeScore == 100;
});
});
});
如果我理解你的要求,我认为这个函数应该起作用:
_.map(trucks, truck => ({
...truck,
wheels: _.filter(_.map(truck.wheels, wheel => ({
...wheel,
mechanics: _.filter(wheel.mechanics, m => m.attributeScore > 90)
})), wheel => wheel.mechanics.length),
}));
这不是一个非常优雅的解决方案,但我最终得到了与您在原始帖子中所希望的相同的答案 如果我理解您的要求,我认为此函数应该起到以下作用:
_.map(trucks, truck => ({
...truck,
wheels: _.filter(_.map(truck.wheels, wheel => ({
...wheel,
mechanics: _.filter(wheel.mechanics, m => m.attributeScore > 90)
})), wheel => wheel.mechanics.length),
}));
这不是一个非常优雅的解决方案,但我最终得到了与您在原始帖子中所希望的相同的答案 你必须更清楚。你到底要过滤什么?你能举一个例子说明你过滤这个例子后会得到什么吗?@OscarPaz。抱歉,现在更新了问题,你必须更清楚。你到底要过滤什么?你能举一个例子说明你过滤这个例子后会得到什么吗?@OscarPaz。抱歉,现在更新了问题我需要应用筛选器后数组的所有内容,但这会切掉父对象并仅返回childYes。父母是反对的对象。所以我们不能在过滤器中切片对象。它只用于数组表示这只返回子数组!!不是嵌套根据您的结构,它将只返回childI在应用筛选器后需要数组的所有内容,但这将分割父对象并仅返回childYes。父母是反对的对象。所以我们不能在过滤器中切片对象。它只用于数组表示这只返回子数组!!不是嵌套根据您的结构,它将返回唯一的子项