I have a problem with figuring out how to find a common values in array of objects.
I have a big array of objects and every 2 objects have the same transactionHash
. I need to find those objects that have the same values and put them in one array.
[
[{...otherData, transactionHash: 1}, {...otherData, transactionHash: 1}]
[{...otherData, transactionHash: 2}, {...otherData, , transactionHash: 2}]
]
I need it to be returned just like that!
I tried to reduce the array:
return yourData.reduce(function(curr, x) {
(curr[x[key]] = curr[x[key]] || []).push(x);
return curr;
})
And surprisingly I got most of the data back organized but somehow the last object wasn't in the right place but the object with the same `transactionHash` exists.
CodePudding user response:
You forgot to pass an initial value for curr
-
return yourData.reduce(function(curr, x) {
(curr[x[key]] = curr[x[key]] || []).push(x);
return curr;
}, {});
If you don't, then the first element of yourData
will be used as the initial value.