我需要通过多个值过滤一个对象。对象示例:items: [{url: "https://...",id: "1693",type: "ABC",currencyCode: "SEK",longName: "Abc",name: "ABC",micCode: "DEF",listingDate: "2018-05-25T00:00:00+02:00",subType: "STOCK", market: { id: "NOROTC"},}.....如果我过滤一个值就可以了:var market = data.filter(item => item.market.id === 'NOROTC');但我需要做的是:var market = data.filter(item => item.market.id === 'NOROTC' && item.market.id === 'NGM');我在 stackoverflow 上发现了一些类似的帖子,但在我的情况下,它们似乎都不起作用。有没有聪明的方法来做到这一点?我也试过 _.filter() 但没有成功......
2 回答
缥缈止盈
TA贡献2041条经验 获得超4个赞
请检查以下示例:
var items = [{
name: 'Amit',
id: 101
},
{
name: 'Amit',
id: 1011
},
{
name: 'Arthit',
id: 102
},
{
name: 'Misty',
id: 103
},
]
var filteredData = items.filter(item => item.name == 'Amit' || item.name== 'Misty');
console.log(filteredData)
慕桂英4014372
TA贡献1871条经验 获得超13个赞
您可以拥有要过滤的 ID 数组并用于Array.includes()过滤数组,如下所示:
var items = [
{market: {id: "NOROTC"}},
{market: {id: "NGM"}},
{market: {id: "foo"}},
{market: {id: "bar"}},
]
var searchItems = ["NOROTC","NGM"]
var filteredData = items.filter(item => searchItems.includes(item.market.id))
console.log(filteredData)
添加回答
举报
0/150
提交
取消