目标是查找“allInvoices”数组中在“selectedIds”数组中具有对应发票Id的所有对象。
我目前正在使用 lodash 并实现如下。我想知道是否有一种更优雅的方法使用 lodash、链接或其他方法来执行此任务。
var selectedIds = [
{ invoiceId: 2, customerId: 34 },
{ invoiceId: 4, customerId: 49}
];
var allInvoices = [
{
id: 2,
name: 'Ron',
date: '2015-14-12'
},
{
id: 3,
name: 'Harry'
},
{
id:4,
name: 'snowpiercer'
},
{
id:5,
name: 'birdman'
},
{
id:6,
name: 'superman'
}
];
var result = [];
_.forEach(selectedIds, function(myId) {
var eachInvoice = _.find(allInvoices,{id: myId.invoiceId});
result.push(eachInvoice);
});
console.log(result);
最佳答案
这是另一种方法:
_(allInvoices)
.groupBy('id')
.at(_.pluck(selectedIds, 'invoiceId'))
.flatten()
.value()
关于javascript - 使用lodash过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30601513/