Javascript -- 比较两个数组,返回差异,但是

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14515382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:50:11  来源:igfitidea点击:

Javascript -- Compare two arrays, return differences, BUT

javascript

提问by Brad Thomas

I've found a lot of posts that solve this problem:

我找到了很多解决这个问题的帖子:

Assuming we have:

假设我们有:

array1 = ['A', 'B', 'C', 'D', 'E']; array2 = ['C', 'E'];

Is there a proven and fast solution to compare two arrays against each other, returning one array without the values appearing in both arrays (C and E here). Desired solution:

是否有一种行之有效的快速解决方案来比较两个数组,返回一个数组而没有出现在两个数组中的值(此处为 C 和 E)。所需的解决方案:

array3 = ['A', 'B', 'D']

But what if you have:

但是如果你有:

array1 = ['A', 'B', 'C', 'D', 'D', 'E']; array2 = ['D', 'E'];

and you're looking for the solution to be:

并且您正在寻找的解决方案是:

array3 = ['A', 'B', 'C', 'D'] // don't wipe out both D's

Here is some context:

这是一些上下文:

You are trying to teach students about how sentences work. You give them a scrambled sentence:

您正在尝试教学生有关句子的工作原理。你给他们乱七八糟的一句话:

ate -- cat -- mouse -- the -- the

吃——猫——老鼠——————

They start typing an answer: The cat

他们开始输入答案:猫

You would like the prompt to now read:

您希望提示现在阅读:

ate -- mouse - the

吃——老鼠——

At present, my code takes out both the's.

目前,我的代码去掉了两个。

Here is what I've tried:
(zsentence is a copy of xsentence that will get manipulated by the code below, join()ed and put to screen)

这是我尝试过的:
(zsentence 是 xsentence 的副本,它将被下面的代码操纵,加入()并放到屏幕上)

for (i=0; i < answer_split.length; i++) {
for (j=0; j < xsentence.length; j++) {
        (function(){
            if (answer_split[i] == xsentence[j]) { zsentence.splice(j,1); return; }
        })();
    }
}

回答by Matt Ball

Just iterate over the array of elements you want to remove.

只需遍历要删除的元素数组。

var array1 = ['A', 'B', 'C', 'D', 'D', 'E'];
var array2 = ['D', 'E'];
var index;

for (var i=0; i<array2.length; i++) {
    index = array1.indexOf(array2[i]);
    if (index > -1) {
        array1.splice(index, 1);
    }
}

It's O(array1.length * array2.length)but for reasonably small arrays and on modern hardware this shouldn't remotely cause an issue.

这是O(array1.length * array2.length)但对于相当小的阵列和在现代硬件上这不应该远程操作,导致的问题。

http://jsfiddle.net/mattball/puz7q/

http://jsfiddle.net/mattball/puz7q/

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/splice

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/splice

回答by Ashish Bhanderi

You can use Filter also. Please review below example.

您也可以使用过滤器。请查看以下示例。

var item = [2,3,4,5];
var oldItems = [2,3,6,8,9];
oldItems = oldItems.filter(n=>!item.includes(n))

so this will return [6,8,9]

所以这将返回 [6,8,9]

and if you want to get only matched items then you have to write below code.

如果您只想获得匹配的项目,则必须编写以下代码。

oldItems = oldItems.filter(n=>item.includes(n))

This will return [2,3] only.

这将仅返回 [2,3]。