Function that removes duplicates from an array.
npm install removeduplicates
var removeDuplicates = require('removeDuplicates');
var tempArray = ["tom", "jack", "jake", "tom"];
var uniqueArray = removeDuplicates(tempArray);
uniqueArray will now equal the following
["tom", "jack", "jake"];
Passing in your array of objects as the first parameter, and the key you're checking against as the second parameter. The function will remove any duplicate values based on this key.
var tempArray = [
{
id: 1234,
selected: true,
otherVal: 'abc'
},
{
id: 5678,
selected: false,
otherVal: 'abc'
},
{
id: 1234,
selected: true,
otherVal: 'def'
}
];
var uniqueArray = removeDuplicates(tempArray, 'id');
uniqueArray will now equal the following
[
{
id: 1234,
selected: true,
otherVal: 'abc'
},
{
id: 5678,
selected: false,
otherVal: 'abc'
}
];