Created
October 26, 2014 23:36
-
-
Save mrded/b2dd21641d5999daf10c to your computer and use it in GitHub Desktop.
JS: How to merge two arrays in Javascript and de-duplicate items
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.unique = function() { | |
var a = this.concat(); | |
for(var i=0; i<a.length; ++i) { | |
for(var j=i+1; j<a.length; ++j) { | |
if(a[i] === a[j]) | |
a.splice(j, 1); | |
} | |
} | |
return a; | |
}; | |
var array1 = [45,46,47,48,49,50,51,52,53,60,123,240,153,284,79,196,61,57, 268,111,129,152,78,342,345,89,110,120,270,350, | |
235,229,242,96,437,236,435,333,487,647,119,397,1018,882,1024,474,237,993,1167,376,380,622, | |
384,1010,1421,1186,1437,790,1644,874,2086,2118,2160,1684,1685,1683,2040,2349,2365,2366, | |
2417,2670,1029,3055,3044,3048,3046,3067,3071,3056,3058,1615,2781,2740,2794,2777,2749,2753, | |
2710,2691,1413,2793,2706,2758,1179,3607,3612,3619,3616,3617, | |
3610,3621,3613,3622,698,726]; | |
var array2 = [45,46,47,49,50,51,51,53,59,60,83,107,122,123,388,395,339,57, 118,129,51,79,336,340,430,333,116,96,251,398, | |
626,268,332,103,504,229,534,382,408,410,563,378,78,461,424,665,555,463,606,474,406,726,706,462,464,480,673, | |
536,675,123,557,770,235,828,650,610,861,865,871,870,656,457,487,742,760,927,775,570,883,403,772,1011,819,1064,879, | |
878,1122,1120,1129,249,376,153,643,1027,647,1073,1114,1115,452,384,334,880,1169,995,785,1014,996,1365,1424,1669, | |
1646,1674,1332,1025,1026,864,1056,2260,1057,646,2384,1206,50,2398,1353,1177,2793,2405,2984,3026,3019,2990,1188,1146,1189]; | |
var array3 = array1.concat(array2).unique(); | |
alert(array3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment