javascript - 如何用另一个数组过滤一个数组

标签 javascript arrays filter

我有一个对象数组:

const array = [{id: 1, bar: "test" }, {id: 2, bar: "test2" }, {id: 3, bar: "test3" }]
我有第二个数组,其中包含要从第一个数组中过滤掉的 ID:
const ids = [1, 2]
如何在没有 ids 中找到的 ID 的情况下创建新的对象数组.

最佳答案

这是一个相当简单的filter手术

const array = [{id: 1, bar: "test" }, {id: 2, bar: "test2" }, {id: 3, bar: "test3" }];

const ids = [1, 2];

var result = array.filter( x => !ids.includes(x.id));
console.log(result);

关于javascript - 如何用另一个数组过滤一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63380584/

相关文章:

javascript - 寻找一种方法来简化带有多个 || 的 if 语句

javascript - JS - 如果某件事花费的时间比 x 长,那么就做某事

javascript - Ext JS 6.2 中的 BufferedStore 和 BufferedRenderer 插件有什么区别

javascript - 如何分别控制两个嵌入的 iframe youtube 视频?

c - 如何在二维数组中找到匹配值的最快方法

c++ - 动态添加一行到 std::string 二维 vector 数组

matlab - 带通滤波器matlab解释

javascript - Mongoose 模型 - TypeError : Account is not a constructor

arrays - 在扩展方法中使用数组类型

file - 调试微过滤器