Remove duplicates from an array of objects in JavaScript

by sambhal

JavaScript

/* https://gist.github.com/telekosmos/3b62a31a5c43f40849bb#gistcomment-2341219 */

uniqueArray = a => [...new Set(a.map(o => JSON.stringify(o)))].map(s => JSON.parse(s));

var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];

var unique = uniqueArray(objects);
console.log(objects);
console.log(unique);