简体   繁体   English

Javascript比较两个不同大小的数组,并返回不在第二个数组中的项目

[英]Javascript Compare two arrays with different sizes and return the items that are not in second array

I have two arrays: 我有两个数组:

var array1 = [a,b,c,d];
var array2 = [1,2,a,b];

I need to have a function that returns an array of the items not in the second Array. 我需要一个函数返回不在第二个数组中的项目数组。

var notInSecond = [c,d];

Does anyone know how to do this? 有谁知道如何做到这一点?

Thanks. 谢谢。

var notInSecond = array1.slice(0); // Creates a clone of array1
for (var i = 0, j; i < array2.length; i++) {
    j = notInSecond.indexOf(array2[i]);
    if (j > -1) notInSecond.splice(j, 1);
}

Keep in mind that indexOf for arrays isn't available for IE8 and lower and it must be emulated. 请记住,数组的indexOf不适用于IE8及更低版本,必须进行模拟。 I'm also assuming that array1 doesn't contain duplicates. 我还假设array1不包含重复项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript,比较不同大小的数组 - javascript, compare arrays of different sizes JavaScript 比较两个 arrays 并将在第二个数组中具有相同属性值的项目推送到第一个数组 - JavaScript compare two arrays and push items to the first array that have the same property value in the second array Angular - 比较两个 arrays 具有相同数量的对象但具有不同的值,并返回与第二个数组的差异 - Angular - Compare two arrays with same number of objects but with different values and return the difference from second array 比较两个数组并更新Javascript中的第二个数组元素 - Compare two arrays and update second array element in Javascript Javascript-比较两个数组并将缺少的项添加到第一个数组中 - Javascript - Compare two arrays and add missing items into first array 比较两个 Arrays 对象,如果 Javascript 中的值为真,则返回一个新数组 - Compare Two Arrays Of Objects and return a new array if value is true in Javascript JavaScript - 合并两个不同大小的 arrays - JavaScript - Merge two arrays with different sizes 比较两个 arrays 并返回一个新数组,其中包含仅在原始 arrays 之一中找到的任何项目 - Compare two arrays and return a new array with any items only found in one of the original arrays 比较不同大小和尺寸的数组 - Compare arrays of different sizes and dimensions 比较两个arrays的对象,判断第一个数组的日期是否在第二个数组Javascript的指定范围内 - Compare two arrays of objects and find out if the date from the first array is within the specified range in the second array in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM