Skip to main content
update url to reflect current url for the Standard ECMA-262
Source Link

Since there is no 'built in' way to remove duplicates (ECMA-262ECMA-262 actually has Array.forEach which would be great for this), we have to do it manually. Note that this pollutes the Array prototype, use with caution.

Since there is no 'built in' way to remove duplicates (ECMA-262 actually has Array.forEach which would be great for this), we have to do it manually. Note that this pollutes the Array prototype, use with caution.

Since there is no 'built in' way to remove duplicates (ECMA-262 actually has Array.forEach which would be great for this), we have to do it manually. Note that this pollutes the Array prototype, use with caution.

Add 2023 answer using `Array.some` and predicate function variable
Source Link

2023 update

The original answer was from years ago. ES6 is fully supported and IE is finally dead. Here's the simplest way to merge primitive and object arrays:

const merge = (a, b, predicate = (a, b) => a === b) => {
    const c = [...a]; // copy to avoid side effects
    // add all items from B to copy C if they're not already present
    b.forEach((bItem) => (c.some((cItem) => predicate(bItem, cItem)) ? null : c.push(bItem)))
    return c;
}

merge(['a', 'b', 'c'], ['c', 'x', 'd']);
// => ['a', 'b', 'c', 'x', 'd']

merge([{id: 1}, {id: 2}], [{id: 2}, {id: 3}], (a, b) => a.id === b.id);
// [{id: 1}, {id: 2}, {id: 3}]

Original answer

Since there is no 'built in' way to remove duplicates (ECMA-262 actually has Array.forEach which would be great for this), we have to do it manually:. Note that this pollutes the Array prototype, use with caution.

Since there is no 'built in' way to remove duplicates (ECMA-262 actually has Array.forEach which would be great for this), we have to do it manually:

2023 update

The original answer was from years ago. ES6 is fully supported and IE is finally dead. Here's the simplest way to merge primitive and object arrays:

const merge = (a, b, predicate = (a, b) => a === b) => {
    const c = [...a]; // copy to avoid side effects
    // add all items from B to copy C if they're not already present
    b.forEach((bItem) => (c.some((cItem) => predicate(bItem, cItem)) ? null : c.push(bItem)))
    return c;
}

merge(['a', 'b', 'c'], ['c', 'x', 'd']);
// => ['a', 'b', 'c', 'x', 'd']

merge([{id: 1}, {id: 2}], [{id: 2}, {id: 3}], (a, b) => a.id === b.id);
// [{id: 1}, {id: 2}, {id: 3}]

Original answer

Since there is no 'built in' way to remove duplicates (ECMA-262 actually has Array.forEach which would be great for this), we have to do it manually. Note that this pollutes the Array prototype, use with caution.

I lost 30 min because I assumed the method was modifying the array in place due to the code example not being explicit, so making this example explicit.
Source Link
AntoineB
  • 4.6k
  • 7
  • 28
  • 65
var array1 = ["Vijendra", "Singh"];
var array2 = ["Singh", "Shakya"];

console.log(array1 = array1.concat(array2);

console.log(array1);
var array1 = ["Vijendra", "Singh"];
var array2 = ["Singh", "Shakya"];

console.log(array1.concat(array2));
var array1 = ["Vijendra", "Singh"];
var array2 = ["Singh", "Shakya"];

array1 = array1.concat(array2);

console.log(array1);
Rollback to Revision 15
Source Link
user229044
  • 237.1k
  • 41
  • 341
  • 342
Loading
Added interactive examples
Source Link
dthree
  • 20.6k
  • 14
  • 79
  • 112
Loading
Commonmark migration
Source Link
Loading
Rollback to Revision 11
Source Link
LiraNuna
  • 66.5k
  • 15
  • 120
  • 141
Loading
added 142 characters in body
Source Link
Black
  • 19.5k
  • 45
  • 178
  • 288
Loading
Added ES6 approach.
Source Link
Bruno João
  • 5.4k
  • 2
  • 22
  • 26
Loading
All browsers support ES5 now, so it's no longer 'progressive'
Source Link
TylerH
  • 21.1k
  • 72
  • 78
  • 105
Loading
Fixed typo in code (Added other text as edits must be 6 chs)
Source Link
Loading
removed unnessary semicolon at end of function.
Source Link
Christophe Roussy
  • 16.7k
  • 4
  • 91
  • 85
Loading
this way result order will be obvious
Source Link
om-nom-nom
  • 62.7k
  • 13
  • 185
  • 230
Loading
Made link direct
Source Link
Qantas 94 Heavy
  • 15.9k
  • 31
  • 71
  • 85
Loading
added 6 characters in body
Source Link
LiraNuna
  • 66.5k
  • 15
  • 120
  • 141
Loading
added 1225 characters in body
Source Link
LiraNuna
  • 66.5k
  • 15
  • 120
  • 141
Loading
The things coding at 4am will do to you...
Source Link
LiraNuna
  • 66.5k
  • 15
  • 120
  • 141
Loading
Fixed order; added 5 characters in body
Source Link
LiraNuna
  • 66.5k
  • 15
  • 120
  • 141
Loading
Source Link
LiraNuna
  • 66.5k
  • 15
  • 120
  • 141
Loading