Unique Values in JSON Object Array
by suraj ahir
JavaScript
var array = [{"email": "[email protected]"}, {"email": "[email protected]"}, {"email": "[email protected]"}, {"email": "[email protected]"}, {"email": "[email protected]"}];
var result = Object.values(array.reduce((a, c) => {
(a[c.email] || (a[c.email] = {email: c.email, rank: 0})).rank++;
return a;
}, {})).sort((a, b) => b.rank - a.rank);
result.forEach((r, i) => r.rank = i + 1);
console.log(result);