Skip to content
Advertisement

Combine json arrays by key, javascript

I need to combine two json arrays, delivered by two rest services. The entries with the same “id” belong together.

JavaScript

I need a combined/copied/cloned json array in javascript in the following way (my model in angular2):

JavaScript

Is there a way to combine them? The parameter names are not defined exactly and it needs to work with variable parameter vectors.

I tried it with mixed for each loops. Seems to me very ugly.

Advertisement

Answer

If you wanted to write it so that you could take in any number of arrays, not just 2, you could utilize arguments, and do something like this:

JavaScript
JavaScript

Here is a working codepen.

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