gpt4 book ai didi

javascript - 从基于另一个数组的数组中删除项目

转载 作者:行者123 更新时间:2023-12-02 16:02:03 27 4
gpt4 key购买 nike

我有两个数组:

var firstArr = [1,2,3,4,5];
var secondArr = [2,3];

如何获得这样的数组:

[1,4,5]

我想使用一种当数组元素是具有多个属性的对象时可以使用的解决方案。

最佳答案

使用filter :

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

例如:

firstArr.filter(function(item){ 
return secondArr.indexOf(item) === -1;
});

关于javascript - 从基于另一个数组的数组中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31101335/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com