Skip to content
Advertisement

Concatenate array of object inside array of object

Suppose I have array of object as:

JavaScript

I want the O/P as:

JavaScript

For this I tried,

JavaScript

It gives the O/p as required but it starts giving value as,

JavaScript

What could be the issue, is there any other way to achieve this?

Advertisement

Answer

See my comment and georg’s, your code works just fine (other than using map incorrectly) provided you want to modify the objects in place.

If you want to create new objects in a new array (e.g., using map correctly), you’d do what you’re doing to get the keys but create a new object with the result, like this:

JavaScript

Live Example:

JavaScript

If bookIssue could have more than one entry (why is it an array if it can’t?) and you wanted all of the entries in bookIssue joined together, you could use map on bookIssue getting all of the keys from its objects and joining them, then join the resulting array:

JavaScript

Live Example:

JavaScript

That also works if there’s just one entry, of course.

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