Skip to main content
edited body
Source Link
simo
  • 15.4k
  • 7
  • 46
  • 61

First concatenate the two arrays, next filter out only the unique items:

var a = [1, 2, 3], b = [101, 2, 1, 10]
var c = a.concat(b)
var d = c.filter((item, pos) => c.indexOf(item) === pos)

console.log(d) // d is [1, 2, 3, 101, 10]

Edit

As suggested a more performance wise solution would be to filter out the unique items in b before concatenating with a:

var a = [1, 2, 3], b = [101, 2, 1, 10]
var c = a.concat(b.filter((item) => a.indexOf(item) < 0))

console.log(c) // dc is [1, 2, 3, 101, 10]

First concatenate the two arrays, next filter out only the unique items:

var a = [1, 2, 3], b = [101, 2, 1, 10]
var c = a.concat(b)
var d = c.filter((item, pos) => c.indexOf(item) === pos)

console.log(d) // d is [1, 2, 3, 101, 10]

Edit

As suggested a more performance wise solution would be to filter out the unique items in b before concatenating with a:

var a = [1, 2, 3], b = [101, 2, 1, 10]
var c = a.concat(b.filter((item) => a.indexOf(item) < 0))

console.log(c) // d is [1, 2, 3, 101, 10]

First concatenate the two arrays, next filter out only the unique items:

var a = [1, 2, 3], b = [101, 2, 1, 10]
var c = a.concat(b)
var d = c.filter((item, pos) => c.indexOf(item) === pos)

console.log(d) // d is [1, 2, 3, 101, 10]

Edit

As suggested a more performance wise solution would be to filter out the unique items in b before concatenating with a:

var a = [1, 2, 3], b = [101, 2, 1, 10]
var c = a.concat(b.filter((item) => a.indexOf(item) < 0))

console.log(c) // c is [1, 2, 3, 101, 10]

deleted 16 characters in body
Source Link
simo
  • 15.4k
  • 7
  • 46
  • 61

First concatenate the two arrays, next filter out only the unique items.:

constvar a = [1, 2, 3],
  b = [101, 2, 1, 10];10]
constvar c = a.concat(b);
constvar d = c.filter((item, pos) => c.indexOf(item) === pos); 

console.log(d); // d is [1, 2, 3, 101, 10]

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 with a:

constvar a = [1, 2, 3],
  b = [101, 2, 1, 10];10]
constvar c = a.concat(b.filter((item) => a.indexOf(item) < 0));

console.log(c); // d is [1, 2, 3, 101, 10]

First concatenate the two arrays, next filter out only the unique items.

const a = [1, 2, 3],
  b = [101, 2, 1, 10];
const c = a.concat(b);
const d = c.filter((item, pos) => c.indexOf(item) === pos);
console.log(d);

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 with a

const a = [1, 2, 3],
  b = [101, 2, 1, 10];
const c = a.concat(b.filter((item) => a.indexOf(item) < 0));

console.log(c);

First concatenate the two arrays, next filter out only the unique items:

var a = [1, 2, 3], b = [101, 2, 1, 10]
var c = a.concat(b)
var d = c.filter((item, pos) => c.indexOf(item) === pos) 

console.log(d) // d is [1, 2, 3, 101, 10]

Edit

As suggested a more performance wise solution would be to filter out the unique items in b before concatenating with a:

var a = [1, 2, 3], b = [101, 2, 1, 10]
var c = a.concat(b.filter((item) => a.indexOf(item) < 0))

console.log(c) // d is [1, 2, 3, 101, 10]

First concatenate the two arrays, next filter out only the unique items.

var a = [1, 2, 3], b = [101, 2, 1, 10];
var c = a.concat(b);
var d = c.filter(function (item, pos) {return c.indexOf(item) == pos});

// d is [1,2,3,101,10]

http://jsfiddle.net/simo/98622/

const a = [1, 2, 3],
  b = [101, 2, 1, 10];
const c = a.concat(b);
const d = c.filter((item, pos) => c.indexOf(item) === pos);
console.log(d);

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 with a

var a = [1, 2, 3], b = [101, 2, 1, 10];
var c = a.concat(b.filter(function (item) {
    return a.indexOf(item) < 0;
}));

// d is [1,2,3,101,10]

const a = [1, 2, 3],
  b = [101, 2, 1, 10];
const c = a.concat(b.filter((item) => a.indexOf(item) < 0));

console.log(c);

First concatenate the two arrays, next filter out only the unique items.

var a = [1, 2, 3], b = [101, 2, 1, 10];
var c = a.concat(b);
var d = c.filter(function (item, pos) {return c.indexOf(item) == pos});

// d is [1,2,3,101,10]

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 with a

var a = [1, 2, 3], b = [101, 2, 1, 10];
var c = a.concat(b.filter(function (item) {
    return a.indexOf(item) < 0;
}));

// d is [1,2,3,101,10]

First concatenate the two arrays, next filter out only the unique items.

const a = [1, 2, 3],
  b = [101, 2, 1, 10];
const c = a.concat(b);
const d = c.filter((item, pos) => c.indexOf(item) === pos);
console.log(d);

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 with a

const a = [1, 2, 3],
  b = [101, 2, 1, 10];
const c = a.concat(b.filter((item) => a.indexOf(item) < 0));

console.log(c);

Update the answer as suggested by comment
Source Link
simo
  • 15.4k
  • 7
  • 46
  • 61
Loading
Source Link
simo
  • 15.4k
  • 7
  • 46
  • 61
Loading