0

Say I have two sets (arrays):

a = [1,2,3];
b = [2,3,5];

and I want 1,2,3,5 out only.

What is the most terse way to do this? Or, failing that, what is a way to do this?

trincot
  • 317,000
  • 35
  • 244
  • 286
toddmo
  • 20,682
  • 14
  • 97
  • 107
  • 1
    Probably answered here: http://stackoverflow.com/questions/3629817/getting-a-union-of-two-arrays-in-javascript – nharrer Sep 29 '15 at 22:09

2 Answers2

0

The following post should help...about half way down is a union with a library reference.

How to merge two arrays in Javascript and de-duplicate items

Community
  • 1
  • 1
jsh
  • 338
  • 1
  • 8
0
var a = [1,2,3];
var b = [2,3,5];

//Concat both arrays [1,2,3,2,3,5]
var result = a.concat(b).filter(function(value, index, self) { //array unique
    return self.indexOf(value) === index; 
});
console.log(result); //[1, 2, 3, 5];
Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
  • `function unique(x) { return x.filter(function(elem, index) { return x.indexOf(elem) === index; }); }; function union(x, y) { return unique(x.concat(y)); };` because I have to reuse it. Thanks! – toddmo Sep 29 '15 at 22:15