You can use array reduce function and inside the reduce call back use findIndex
to check if the accumulator array have an object with same tag. If a object with same tag is found then update the counter in that object , otherwise push the current object in the accumulator array
let data = [{
"tag": "#sala",
"state": {
"counter": 1
}
},
{
"tag": "#sala",
"state": {
"counter": 2
}
}
];
let newData = data.reduce(function(acc, curr) {
let findTagIndex = acc.findIndex(item => item.tag === curr.tag);
if (findTagIndex === -1) {
acc.push(curr)
} else {
acc[findTagIndex].state.counter += curr.state.counter
}
return acc;
}, []);
console.log(newData)
1
solved Merge and sum array objects Javascript