Skip to content
Advertisement

Javascript coding, combine the same object

data = [{a:1, b:2},{a:2, b:2},{a:2, b:2},{a:3, b:2},{a:3, b:2},{a:3, b:2}], we need to convert data array to data = [{a:1, b:2, count:1},{a:2, b:2, count:2},{a:3, b:2, count:3}]

I used this code, but it seems the key to the map is deep comparison. so even with the same string value, I got different keys

JavaScript

I also do an iteration of the data array, but the same problem occurs.

JavaScript

So is there a better way to duel with it?

Advertisement

Answer

Comparing objects will require checking that the number of keys is the same, and that each key-value pair exists in both. It’ll require a bit of boilerplate.

JavaScript
Advertisement