I want to find number a duplicates and it's name in array of objects in javascript.
I checked related questions but none help in finding the exact duplicate count. Even they do not focus on finding it if there are multiple duplicates
I tried this which will throw message on finding duplicates. But i want the count of duplicates and handle the case when there are multiple duplicates with count of each name occuring multiple times.
My code:
let keyArr = this.customPropertiesArray.map(function (item: any) { return item.name });
let isDuplicate = keyArr.some(function (item: any, idx: any) {
return keyArr.indexOf(item) != idx
});
if (isDuplicate) {
console.log("Duplicate found");
}
The array could be something like this:
let array=[
{"name":"name1","value":"value1"},
{"name":"name2","value":"value2"},
{"name":"name1","value":"value42"},
{"name":"name2","value":"value52"},
{"name":"name3","value":"value2"},
{"name":"name2","value":"value2"}...];
I want a message something like "name1 is duplicate occuring 2 times" and "name2 is duplicate occuring 3 times"