如何对来自另一个数组的所有元素的数组进行部分匹配筛选 [英] How to do a partial match filter on an array from all elements of another array

查看:0
本文介绍了如何对来自另一个数组的所有元素的数组进行部分匹配筛选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从另一个数组的整个中筛选具有部分匹配的数组。例如,数组概述如下:

Array1 = 
     categories: 292300, 
     categories: 300, 
     categories: 292500280

Array2 = 
     300,
     498

使用筛选器,我希望返回:

NewArray = 
     categories: 292300, 
     categories: 300

实现这一点的最佳方式是什么?我已经尝试了下面的代码,但没有成功:

  const NewArray = Array1.filter(Array1 => !(Array1.categories.includes(Array2)))

推荐答案

要进行部分匹配,只需解析inttostring

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
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}

这篇关于如何对来自另一个数组的所有元素的数组进行部分匹配筛选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆