从两个数组中获取唯一值并将它们放在另一个数组中 - Jquery [duplicate]

时间:2021-11-29 12:21:50

This question already has an answer here:

这个问题在这里已有答案:

I have two arrays in javascript -:

我在javascript中有两个数组 - :

var array1 = ['12','1','10','19','100'];
var array2 = ['12','10','19'];

I need to a method to get the unique from two arrays and put them in array3 Array3 should be -: var array3 = ['1','100'];

我需要一个方法来从两个数组中获取唯一的并将它们放在array3中,Array3应该是 - :var array3 = ['1','100'];

Thanks for help.

感谢帮助。

7 个解决方案

#1


25  

var array3 = array1.filter(function(obj) { return array2.indexOf(obj) == -1; });

MDN on Array#filter: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter

阵列#过滤器上的MDN:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter

Includes a polyfill for older browsers.

包含旧版浏览器的polyfill。

#2


6  

var unique = [];
for(var i = 0; i < array1.length; i++){
    var found = false;

    for(var j = 0; j < array2.length; j++){ // j < is missed;
     if(array1[i] == array2[j]){
      found = true;
      break; 
    }
   }
   if(found == false){
   unique.push(array1[i]);
  }
}

#3


3  

With a bit of ES6 magic it can be fairly concise. Note that we need to check both ways round incase there are unique items in either array.

有了一点ES6魔力,它可以相当简洁。请注意,我们需要检查两种方式,因为任何一个数组中都有唯一的项目。

const arr1 = [1,2,3,4,5];
const arr2 = [1,3,8];

let unique1 = arr1.filter((o) => arr2.indexOf(o) === -1);
let unique2 = arr2.filter((o) => arr1.indexOf(o) === -1);

const unique = unique1.concat(unique2);

console.log(unique);
// >> [ 2, 4, 5, 8]

#4


2  

Something like this

像这样的东西

var array1 = ['12','1','10','19','100'];
var array2 = ['12','10','19'];
var o = {};
for(var i in array1) {
    o[i] = 1;
}
for(var i in array2) {
    o[i] = 0;
}
var array3 = [];
for(var i in o) {
    if(o[i] == 1) {
        array3.push(i);
    }
}

#5


0  

Similar to above but will work with more than two arrays

与上面类似,但可以使用两个以上的数组

var array1 = ['12','1','10','19','100'];
var array2 = ['12','10','19'];
var i = 0;
var hist = {};
var array3 = [];

buildhist(array1);
buildhist(array2);

for (i in hist) {
    if (hist[i] === 1) {
        array3.push(i);
    }
}

console.log(array3);

function buildhist(arr) {
    var i;
    for (i = arr.length - 1; i >= 0; i--) {
        if (hist[arr[i]] === undefined) {
            hist[arr[i]] = 0;
        }
        hist[arr[i]]++;
    }
}

#6


0  

var array3 = array1.concat(array2);

array3 = array3.sort(function(a, b) { return a > b; });
array3 = array3.filter(function(num, index) { return num !== array3[index + 1]; });

array3 will have only unique values

array3只有唯一值

this also does the job in two loops which is pretty inexpensive, it should be noted that sort() and filter() are ECMA5 functions and not supported in older browsers, also i usually use a library like underscore so i don't have rewrite these functions for each project i work on, underscore has a .unique() method which obviously is less code and more clearly states the intention of the operation

这也是两个循环中的工作相当便宜,应该注意sort()和filter()是ECMA5函数,在旧版浏览器中不受支持,我也经常使用像下划线这样的库,所以我没有重写对于我正在处理的每个项目的这些函数,下划线都有一个.unique()方法,显然代码更少,更清楚地说明了操作的意图

#7


0  

var array1 = ['12','1','10','19','100'];
var array2 = ['12','10','19'];


var newArr,temp,temp1;


    temp=array1.filter(function(el) 
              {
                return arr2.indexOf(el) == -1; 

              });

    temp1=array2.filter(function(el) 
              {
                return arr1.indexOf(el) == -1; 

              });

  newArr=temp.concat(temp1);


  return newArr;

}

#1


25  

var array3 = array1.filter(function(obj) { return array2.indexOf(obj) == -1; });

MDN on Array#filter: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter

阵列#过滤器上的MDN:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter

Includes a polyfill for older browsers.

包含旧版浏览器的polyfill。

#2


6  

var unique = [];
for(var i = 0; i < array1.length; i++){
    var found = false;

    for(var j = 0; j < array2.length; j++){ // j < is missed;
     if(array1[i] == array2[j]){
      found = true;
      break; 
    }
   }
   if(found == false){
   unique.push(array1[i]);
  }
}

#3


3  

With a bit of ES6 magic it can be fairly concise. Note that we need to check both ways round incase there are unique items in either array.

有了一点ES6魔力,它可以相当简洁。请注意,我们需要检查两种方式,因为任何一个数组中都有唯一的项目。

const arr1 = [1,2,3,4,5];
const arr2 = [1,3,8];

let unique1 = arr1.filter((o) => arr2.indexOf(o) === -1);
let unique2 = arr2.filter((o) => arr1.indexOf(o) === -1);

const unique = unique1.concat(unique2);

console.log(unique);
// >> [ 2, 4, 5, 8]

#4


2  

Something like this

像这样的东西

var array1 = ['12','1','10','19','100'];
var array2 = ['12','10','19'];
var o = {};
for(var i in array1) {
    o[i] = 1;
}
for(var i in array2) {
    o[i] = 0;
}
var array3 = [];
for(var i in o) {
    if(o[i] == 1) {
        array3.push(i);
    }
}

#5


0  

Similar to above but will work with more than two arrays

与上面类似,但可以使用两个以上的数组

var array1 = ['12','1','10','19','100'];
var array2 = ['12','10','19'];
var i = 0;
var hist = {};
var array3 = [];

buildhist(array1);
buildhist(array2);

for (i in hist) {
    if (hist[i] === 1) {
        array3.push(i);
    }
}

console.log(array3);

function buildhist(arr) {
    var i;
    for (i = arr.length - 1; i >= 0; i--) {
        if (hist[arr[i]] === undefined) {
            hist[arr[i]] = 0;
        }
        hist[arr[i]]++;
    }
}

#6


0  

var array3 = array1.concat(array2);

array3 = array3.sort(function(a, b) { return a > b; });
array3 = array3.filter(function(num, index) { return num !== array3[index + 1]; });

array3 will have only unique values

array3只有唯一值

this also does the job in two loops which is pretty inexpensive, it should be noted that sort() and filter() are ECMA5 functions and not supported in older browsers, also i usually use a library like underscore so i don't have rewrite these functions for each project i work on, underscore has a .unique() method which obviously is less code and more clearly states the intention of the operation

这也是两个循环中的工作相当便宜,应该注意sort()和filter()是ECMA5函数,在旧版浏览器中不受支持,我也经常使用像下划线这样的库,所以我没有重写对于我正在处理的每个项目的这些函数,下划线都有一个.unique()方法,显然代码更少,更清楚地说明了操作的意图

#7


0  

var array1 = ['12','1','10','19','100'];
var array2 = ['12','10','19'];


var newArr,temp,temp1;


    temp=array1.filter(function(el) 
              {
                return arr2.indexOf(el) == -1; 

              });

    temp1=array2.filter(function(el) 
              {
                return arr1.indexOf(el) == -1; 

              });

  newArr=temp.concat(temp1);


  return newArr;

}