使用另一个数组从数组中删除条目 [英] Remove entry from array using another array
本文介绍了使用另一个数组从数组中删除条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
不确定怎么做,所以非常感谢任何帮助
Not sure how do to this, so any help is greatly appreciated
说我有:
const array1 = [1, 1, 2, 3, 4];
const array2 = [1, 2];
期望输出
const result = [1, 3, 4];
我希望比较 array1
和 array2
并且对于 array2
中的每个条目,从 array1
中删除等效项。因此,如果我在 array1
中有3个,而在 array2
中有1个,则生成的数组应为2 。
I wish to compare array1
and array2
and for each entry in array2
, remove the equivalent from array1
. So if I have 3 of 1 in array1
and 1 of 1 in array2
, the resulting array should have 2 of 1.
处理同时具有jquery和underscore.js的项目,如果这样做更容易。
Working on a project that has both jquery and underscore.js if that makes anything easier.
推荐答案
var array1 = [1, 1, 2, 3, 4],
array2 = [1, 2],
result = array1.slice(0);
array2.forEach(function(element) {
var index = result.indexOf(element)
if (index >= 0) {
result.splice(index, 1)
}
})
console.log(result)
这篇关于使用另一个数组从数组中删除条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文