How do I modify an array of objects so that duplicate names are merged. Consider the example
const arr1 = [
{name: "HTML", count: 4},
{name: "CSS", count: 5},
{name: "JS", count: 10},
{name: "CSS", count: 11},
{name: "PHP", count: 12},
{name: "HTML", count: 4},
{name: "CSS", count: 14},
{name: "JS", count: 7}]
My resulting array should be
const result = [
{name: "HTML", count: 8},
{name: "CSS", count: 30},
{name: "JS", count: 17},
{name: "PHP", count: 12}]
In this case, the count
property gets added for objects having same name
.
Can someone please tell how I can do this
(I would also like to have an explanation for the code because I am a bit new to using higher order functions)
Any help is appreciated, Thanks