I may use the implicit variables so that you could read easily, see my comments for the explanations
// get the input property to group by, we attach the function to the prototype objectArray.prototype.groupBy=function(prop){// then apply reducereturnthis.reduce((group,item)=>{// create new property for the group object if not existed, if yes, reassign from the accumulator group[item[prop]]=group[item[prop]]||[];// add new item to the group of property (in this case, a list) we want to groupgroup[item[prop]].push(item);returngroup;// create initial empty object to implement reduce},{});};
Then we can use people.groupBy('gender') or people.groupBy('age')
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
A more generic solution to group by any property:
Didn't understand It can you explain a little more.
I may use the implicit variables so that you could read easily, see my comments for the explanations
Then we can use
people.groupBy('gender')
orpeople.groupBy('age')