Skip to content
Advertisement

Combine objects in a json using javascript

Having a JSON in this format:

JavaScript

how can I combine the objects by name, country, and countryID in Javascript to get the following JSON output?

JavaScript

Advertisement

Answer

Using Array.prototype.reduce, you can group array items by country and countryID key-value pairs and store the result to the object values of that generated key as follows.

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