Wednesday, June 17, 2015

Remove duplicates in an array


[1,1,2,2,3,3,4].reduce(function (a,b) { if (a.indexOf(b) < 0) a.push(b); return a;},[]);

will return [1,2,3,4]

In case of arrays composed of JSON objects use this expression for the "if"clause:

(JSON.stringify(a).indexOf(JSON.stringify(b))