我需要按多个值过滤对象。
对象示例:
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(),但没有成功...
答案 0 :(得分:1)
您可以拥有一个要过滤的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)
希望这会有所帮助。