如何对来自另一个数组的所有元素的数组进行部分匹配筛选
我正在尝试从另一个数组的整个中筛选具有部分匹配的数组。例如,数组概述如下:
Array1 =
categories: 292300,
categories: 300,
categories: 292500280
Array2 =
300,
498
使用筛选器,我希望返回:
NewArray =
categories: 292300,
categories: 300
实现这一点的最佳方式是什么?我已经尝试了下面的代码,但没有成功:
const NewArray = Array1.filter(Array1 => !(Array1.categories.includes(Array2)))
解决方案
要进行部分匹配,只需解析int
tostring
const arr1 = [{categories: 292300}, {categories: 300}, {categories: 292500280}];
const arr2 = [300, 498];
const result = arr1.filter(({ categories }) =>
arr2.some((e) => String(categories).includes(String(e))));
console.log(result);
.as-console-wrapper {max-height: 100% !important; top: 0}
相关文章