Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find a common values in array of objects and organize them

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading