gpt4 book ai didi

javascript - 如何使用下划线javascript过滤数组数组

转载 作者:行者123 更新时间:2023-11-28 02:19:31 25 4
gpt4 key购买 nike

我有一个对象数组如下:

[{name:'Initiative 1', actionList:[{name:'action 1', productionLine:'436767'}]}, {name:'Initiative 2', actionList:[{name:'action 2', productionLine:'892128'}]}]

我想根据 productionLine 值根据 ex 的 actionList 元素的某些属性过滤结果。

这是我的代码,它没有按预期工作,我得到一个空结果。

initiatives.forEach(function(initiative) {
var newArr = _.filter(initiative.actionList, function({
return o.productionLine == selectedProductionLine;
});
initiative.actionList = newArr;
});

预期:输入给定:892128结果:

[{name:'Initiative 1', actionList:[]}, {name:'Initiative 2', actionList:[{name:'action 2', productionLine:'892128'}]}]

最佳答案

语法看起来不适合你的过滤器:

var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])

查看 Array.prototype.filter()

下面的代码片段应该能满足您的需求。

let initiatives = [{
name: 'Initiative 1',
actionList: [{
name: 'action 1',
productionLine: '436767'
}]
}, {
name: 'Initiative 2',
actionList: [{
name: 'action 2',
productionLine: '892128'
}]
}]

let selectedProductionLine = '892128';

initiatives.forEach(function(initiative) {
newArray = initiative.actionList.filter(function(actionList) {
return actionList.productionLine == selectedProductionLine;
});
initiative.actionList = newArray
});

console.log(initiatives);

关于javascript - 如何使用下划线javascript过滤数组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58702554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com