I have two JavaScript arrays:
var array1 = ["Vijendra","Singh"];
var array2 = ["Singh", "Shakya"];
I want the output to be:
var array3 = ["Vijendra","Singh","Shakya"];
The output array should have repeated words removed.
How do I merge two arrays in JavaScript so that I get only the unique items from each array in the same order they were inserted into the original arrays?
First concatenate the two arrays, next filter out only the unique items.
http://jsfiddle.net/simo/98622/
Edit
As suggested by @Dmitry (see the second comment below) a more performance wise solution would be to filter out the unique items in
b
before concatenating witha
My one and a half penny:
In Dojo 1.6+
Update
See working code.
http://jsfiddle.net/UAxJa/1/
Merge an unlimited number of arrays or non-arrays and keep it unique:
Assuming original arrays don't need de-duplication, this should be pretty fast, retain original order, and does not modify the original arrays...
usage:
It can be done using Set.