1function getUnique(arr, comp) {
2
3 // store the comparison values in array
4 const unique = arr.map(e => e[comp])
5
6 // store the indexes of the unique objects
7 .map((e, i, final) => final.indexOf(e) === i && i)
8
9 // eliminate the false indexes & return unique objects
10 .filter((e) => arr[e]).map(e => arr[e]);
11
12 return unique;
13}
14
15console.log(getUnique(arr,'id'));
1var arrayWithDuplicates = [
2 {"type":"LICENSE", "licenseNum": "12345", state:"NV"},
3 {"type":"LICENSE", "licenseNum": "A7846", state:"CA"},
4 {"type":"LICENSE", "licenseNum": "12345", state:"OR"},
5 {"type":"LICENSE", "licenseNum": "10849", state:"CA"},
6 {"type":"LICENSE", "licenseNum": "B7037", state:"WA"},
7 {"type":"LICENSE", "licenseNum": "12345", state:"NM"}
8];
9
10function removeDuplicates(originalArray, prop) {
11 var newArray = [];
12 var lookupObject = {};
13
14 for(var i in originalArray) {
15 lookupObject[originalArray[i][prop]] = originalArray[i];
16 }
17
18 for(i in lookupObject) {
19 newArray.push(lookupObject[i]);
20 }
21 return newArray;
22 }
23
24var uniqueArray = removeDuplicates(arrayWithDuplicates, "licenseNum");
25console.log("uniqueArray is: " + JSON.stringify(uniqueArray));
1const things = {
2 thing: [
3 { place: 'here', name: 'stuff' },
4 { place: 'there', name: 'morestuff1' },
5 { place: 'there', name: 'morestuff2' },
6 ],
7};
8
9const removeDuplicates = (array, key) => {
10 return array.reduce((arr, item) => {
11 const removed = arr.filter(i => i[key] !== item[key]);
12 return [...removed, item];
13 }, []);
14};
15
16console.log(removeDuplicates(things.thing, 'place'));
17// > [{ place: 'here', name: 'stuff' }, { place: 'there', name: 'morestuff2' }]
18