Last active
May 8, 2023 03:17
-
-
Save rogerramosme/6658eff05c7c44ec9bd58fa423380556 to your computer and use it in GitHub Desktop.
JavaScript: Count duplicates in an array of objects by property name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const users = [ | |
{ | |
id: 60, | |
name: "roger" | |
}, | |
{ | |
id: 60, | |
name: "roger" | |
}, | |
{ | |
id: 60, | |
name: "roger" | |
}, | |
{ | |
id: 24, | |
name: "fabio" | |
}, | |
{ | |
id: 24, | |
name: "fabio" | |
}, | |
{ | |
id: 30, | |
name: "guilherme" | |
}, | |
{ | |
id: 60, | |
name: "roger" | |
} | |
]; | |
/** | |
* Returns a unique array by prop name | |
* @param {array} array - array that will be compared | |
* @param {string} compareProp - property that will be compared across the array | |
* @returns new array of objects with uniques and a new prop with number of occurrences | |
* @example: | |
* const users = [{name: 'roger', id: 1}, {name: 'roger', id: 1}, {name: 'matheus', id: 2}]; | |
* getUniques(users, 'id'); | |
* // [{name: 'roger', id: 1, count: 2}, {name: 'matheus', id: 2, count: 1}] | |
*/ | |
const getUniques = (array, compareProp) => { | |
let byId = {}; | |
let uniques = []; | |
let i = 0; | |
let item = {}; | |
for (i; i < array.length; i++) { | |
item = array[i]; | |
if (byId[item[compareProp]]) { | |
byId[item[compareProp]].count++; | |
} else { | |
byId[item[compareProp]] = item; | |
uniques.push(item); | |
item.count = 1; | |
} | |
} | |
return uniques; | |
}; | |
console.table(getUniques(users, 'id')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment