Skip to content

Instantly share code, notes, and snippets.

@rogerramosme
Last active May 8, 2023 03:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogerramosme/6658eff05c7c44ec9bd58fa423380556 to your computer and use it in GitHub Desktop.
Save rogerramosme/6658eff05c7c44ec9bd58fa423380556 to your computer and use it in GitHub Desktop.
JavaScript: Count duplicates in an array of objects by property name
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