Skip to content
Advertisement

Efficient way to merge two array of objects in javascript based on similarities?

So I have two array of objects:

JavaScript

and the expected result is:

JavaScript

This is just two examples out of thousands of data, I have tried mapping through two arrays and inserting them manually however this would always result in a timeout since I think looping is quite heavy on the backend server. Is there an efficient way to do such an operation or a lightweight solution in packages like lodash to achieve this?

Advertisement

Answer

Unless you’re using native module, lodash and other packages will also loop over the values internally.

Assuming that you’re using id to group the elements together, this is probably what you want

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement