Skip to content
Advertisement

Spreading an array inside an object gives unexpected result – Javascript

I have two arrays of objects with the same keys and I want to combine them and create a new array of objects. I am able to get almost everything right, but the issue occurs when I want to spread them inside the new array. The spread automatically includes a key value pair with the index and the value which is not what i want. What am I missing? Please advice.

This is my implementation:

JavaScript

Expected Output:

JavaScript

Advertisement

Answer

The type of resp in the code snippet before is an array of objects.

JavaScript

You likely want the type to be an object directly.

You can use Array.reduce for this:

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