簡體   English   中英

獲取在Java中比較兩個或多個數組的Common和Uniques元素

[英]Get the Common and Uniques Elements Comparing two or more arrays in Javascript

首先,我檢查了很多帖子,像這樣:

查找多個JavaScript數組之間的匹配項

如何在Javascript中合並兩個數組並刪除重復項

數組中的唯一值

所有這些都可以正常工作,但僅適用於包含整數或字符串的數組,我需要適用於您想要的任何對象。

我有兩個數組,想在一個新數組中存儲唯一元素,而在其他數組中存儲公共元素。 這些要素可能是; 變量,數組,字符串,哈希(對象)函數,整數,浮點數或布爾值

並且可能永遠不會在數組內部成為重復值。 普通JS會很棒

而且我不關心IE(但是我想對其他人會很好),所以如果有一種新的ES6方式,我會喜歡的,我更關心性能了:)

// Some values that gonna be inside the array1 & array2
function random(){};
var a = 5,
    b = {};


// The Arrays
var array1 = [0,1,2,3, "HeLLo", "hello", 55.32, 55.550, {key: "value", keyWithArray: [1,2,3]}, random, a, b];
var array2 = [2,3, "hello", "Hello", 55.32, 55.551, {key: "value", keyWithArray: [1,2,3]}, b];



// The Unique Array should be all the elements that array1 have and array2 haven't
var uniqueArray = [0, 1, "HeLLo", 55.550, random, a];


// The commonArray should the common elements in both arrays (array1 and array2)
var commonArray = [2,3, "hello", 55.32, {key: "value", keyWithArray: [1,2,3]}, b]




// I try something like this but doesn't work
var uniqueArray = array1.filter(function(val) { return array2.indexOf(val) == -1; });
console.log(uniqueArray);

據我了解,您基本上想對兩個陣列執行一些設置操作。 我的建議是首先從兩個數組中構建一個更合適的數據結構,因為要做類似的事情,則必須執行O(n²)算法。 這樣的事情應該做到:

// convert a plain array that has values of mixed types to and object
// where the keys are the values in plain form in case of strings, scalars or functions, or serialized objects in
// case of objects and arrays, and where the values are the unaltered values of the array.
var _toObject = function(arr) {
    var obj = {};
    for (var i=0 ; i<arr.length ; i++) {
        var el = arr[i];
        var type = typeof el;
        if (type !== 'object') { // scalars, strings and functions can be used as keys to an array
            obj[el] = el;
        }
        else { // objects and arrays have to be serialized in order to be used as keys
            obj[JSON.stringify(el)] = el;
        }
    };
    return obj;
};


var objArray1 = _toObject(array1);
var objArray2 = _toObject(array2);

var uniqueArray = [];
var commonArray = [];
for (var i in objArray1) {
    if (i in objArray2) {
        commonArray.push(objArray1[i]); // push the common elements

        delete objArray2[i]; // delete so in the end objArray2 will only have unique elements
    }
    else {
        uniqueArray.push(objArray1[i]); // push unique element from objArray1
    }
}

for (var i in objArray2) { // now objArray2 has only unique values, just append then to uniqueArray
    uniqueArray.push(objArray2[i])
}

console.log('Unique array', uniqueArray);
console.log('Common array', commonArray);

這應該給您想要的結果:

bash-4.2$ node test.js 
Unique array [ 0, 1, 5, 'HeLLo', 55.55, [Function: random], 'Hello', 55.551 ]
Common array [ 2, 3, 'hello', 55.32, { key: 'value', keyWithArray: [1, 2, 3 ] }, {}]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM