Skip to content
Advertisement

Map a new key-value pair into an array of objects

I would like to add a new key-value pair to all objects within my data. The structure looks like this:

JavaScript

As a new key-value pair, I would like to add “logged-in”: true/false. To get this data, I use another service. And then I map the true/false values into a variable.

JavaScript

Then, I would like to push these key-value pairs into userData.

JavaScript

What I would like to achieve:

JavaScript

What did I do wrong here?

Advertisement

Answer

map returns new data, so your function should look something like this

JavaScript

If you want to modify this.userData directly, you can use other methods like forEach

JavaScript
Advertisement