1let person = [{name: "john"}, {name: "jane"}, {name: "imelda"}, {name: "john"}];
2
3function removeDuplicates(data, key) {
4
5 return [
6 ...new Map(data.map(item => [key(item), item])).values()
7 ]
8
9};
10
11console.log(removeDuplicates(person, item => item.name));
1const arr = [{id: 1, name: 'one'}, {id: 2, name: 'two'}, {id: 1, name: 'one'}]
2
3const ids = arr.map(o => o.id)
4const filtered = arr.filter(({id}, index) => !ids.includes(id, index + 1))
5
6console.log(filtered)
1let person = [
2{name: "john"},
3{name: "jane"},
4{name: "imelda"},
5{name: "john"},
6{name: "jane"}
7];
8
9const data = Array.from(new Set(person.map(JSON.stringify))).map(JSON.parse);
10console.log(data);
1let person = [
2{name: "john"},
3{name: "jane"},
4{name: "imelda"},
5{name: "john"},
6{name: "jane"}
7];
8
9const obj = [...new Map(person.map(item => [JSON.stringify(item), item])).values()];
10console.log(obj);
1let days = ["senin","senin","selasa","selasa","rabu","kamis", "rabu"];
2let fullname = [{name: "john"}, {name: "jane"}, {name: "imelda"}, {name: "john"},{name: "jane"}];
3
4// REMOVE DUPLICATE FOR ARRAY LITERAL
5const arrOne = new Set(days);
6console.log(arrOne);
7
8const arrTwo = days.filter((item, index) => days.indexOf(item) == index);
9console.log(arrTwo);
10
11
12// REMOVE DUPLICATE FOR ARRAY OBJECT
13const arrObjOne = [...new Map(person.map(item => [JSON.stringify(item), item])).values()];
14console.log(arrObjOne);
15
16const arrObjTwo = Array.from(new Set(person.map(JSON.stringify))).map(JSON.parse);
17console.log(arrObjTwo);
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));