更新时间:2023-01-15 17:44:45
您的第二个数组无效.您必须用引号将字符串值引起来.
Your second array is not valid. You have to wrap the string values with quotes.
您可以使用 Array.prototype.filter()
You can use Array.prototype.filter()
filter()方法创建一个新数组,其中包含所有通过提供的功能实现的测试的元素.
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
includes()方法确定数组是否包含某个元素,并在适当时返回true或false.
The includes() method determines whether an array includes a certain element, returning true or false as appropriate.
尝试以下方式:
var arr1 = [1, 3, 4,];
var arr2 = [
{
id: 1,
title: 'Title 1',
},
{
id: 2,
title: 'Title 2',
},
];
var res = arr2.filter(i => arr1.includes(i.id));
console.log(res);