JavaScript: Remove duplicates of objects sharing same property value JavaScript: Remove duplicates of objects sharing same property value angularjs angularjs

JavaScript: Remove duplicates of objects sharing same property value


This function removes duplicate values from an array by returning a new one.

function removeDuplicatesBy(keyFn, array) {    var mySet = new Set();    return array.filter(function(x) {        var key = keyFn(x), isNew = !mySet.has(key);        if (isNew) mySet.add(key);        return isNew;    });}var values = [{color: "red"}, {color: "blue"}, {color: "red", number: 2}];var withoutDuplicates = removeDuplicatesBy(x => x.color, values);console.log(withoutDuplicates); // [{"color": "red"}, {"color": "blue"}]