0

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.

enter image description here

1 Answer 1

0

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.