Javascript: Compare three arrays -
i want compare many arrays , combine identical:
a = [1,2,3]; b = [1,2,3]; c = [1,2,3]; d = [10,11,12]; e = [10,11,12]; f = [10,11,12]; g = [13,14]; h = [13,14];
if there identical arrays i'd create new arrays out of identical ones:
i = [1,2,3]; j = [10,11,12]; k = [13,14];
would need iterate through each element in 1 array against of elements in other arrays?
for (var in a) { (var j in b) { if (a[i] == j[j]) { // create new arrays } } }
etc...
then, create new arrays out of matches? sounds lot of overhead.
what's best way accomplish this?
thanks!
if you're trying finish unique arrays, use hash approach:
var myarrays = [a,b,c,d,e,f,g], uniques = [], hashes = {}; (var i=0; < myarrays.length; i++) { var hash = json.stringify(myarrays[i]); // or .tostring(), or whatever if (!(hash in hashes)) { hashes[hash] = true; uniques.push(myarrays[i]); } } // uniques holds unique arrays
Comments
Post a Comment