const endorsedSkills = [
{
language: "Javascript",
user: 'Aarav'
},
{
language: "ReactJS",
user: 'Aarush'
},
{
language: "Javascript",
user: 'Aaryan'
},
{
language: "ReactJS",
user: 'Aayansh'
},
{
language: "Angular",
user: 'Aayush'
},
{
language: "Javascript",
user: 'Aayansh'
},
{
language: "Angular",
user: 'Adhrit'
},
{
language: "Javascript",
user: 'Adi'
},
{
language: "ReactJS",
user: 'Pranav'
},
];
We are given an array which contains list of objects where each one of them represents a language and user it was endorsed by.
If we have to modify this array by grouping them by language so that we can display list of languages with endorsed by users list and count in our UI,So output would be something like this
[
{
"language": "Javascript",
"user": [
"Aarav",
"Aaryan",
"Aayansh",
"Adi"
],
"count": 4
},
{
"language": "ReactJS",
"user": [
"Aarush",
"Aayansh",
"Pranav"
],
"count": 3
},
{
"language": "Angular",
"user": [
"Aayush",
"Adhrit"
],
"count": 2
}
]
How do we do it efficiently without any external libraries?
Answer is by using Array.reduce() method.reduce method is very useful in scenario like this.
Basic idea behind this is very simple, We keep an empty array as an accumulator, While iterating over each element of an array we check if element is already present if yes, then we update existing item with that particular index value if no, then we create a new item
const formattedArray = endorsedSkills.reduce((acc, cur) => {
const index = acc.findIndex((ele) => ele.language == cur.language);
if (index != -1) {
acc[index].user.push(cur.user);
acc[index].count++;
} else {
acc.push({ language: cur.language, user: [cur.user], count: 1 });
}
return acc;
}, []);
console.log('formattedArray', formattedArray);
Hope this helps someone who is looking for something similar.
Top comments (2)
Or the less readable version
MDN:
Array.prototype.groupBy()
Array Grouping Proposal
Currently at Stage 3
Currently polyfilled by core-js or perhaps something like
to be replaced later.
I'm looking forward to this one being better supported!