我有一个数组,我想根据多个值对其进行过滤:
let books= [{ bookId : 1},{ bookId : 3},{ bookId : 4},{ bookId : 7},{ bookId : 10}];
let searchValues= [1,7];
let newArray = [];
我可以像下面这样使用散布运算符:
searchValues.map(book=> {
newArray.push(...books.filter(f => f.bookId== book.id));
});
但是有一种方法可以使用像这样或类似的东西衬里来过滤阵列? :
newArray = books.filter(f => f.bookId in values);
谢谢。
答案 0 :(得分:3)
newArray = books.filter(f => values.includes(f.bookId))
let books = [
{ bookId: 1 },
{ bookId: 3 },
{ bookId: 4 },
{ bookId: 7 },
{ bookId: 10 },
]
let searchValues = [1, 7]
let newArray = []
newArray = books.filter((f) => searchValues.includes(f.bookId))
console.log(newArray)