Reputation: 2400
First of all i check a lot of post, like this:
Finding matches between multiple JavaScript Arrays
How to merge two arrays in Javascript and de-duplicate items
All these works fine but only with arrays that contain integers or strings, i need that works for whatever you want.
i have two arrays, and want to storage in a new array the unique elements, and in other array the common elements. These elements could be; variables, arrays, strings, hash (objects) functions, integers, float numbers or boolean
And probably never gonna be a duplicate value inside of the arrays. Would be awesome with plain JS
And i don't care about IE (but would be nice for others i guess), so if there is a new ES6 way, i gonna love it , i care more about performance :)
// 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);
Upvotes: 0
Views: 178
Reputation: 909
From what I understood you basically want to perform some set operations on your two arrays. My suggestion is to first build a more appropriate data structure from your two arrays, because to do something like get the intersection of both you would have to do an O(n²) algorithm. Something like this should do it:
// 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);
this should give you the desired result:
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 ] }, {}]
Upvotes: 1