Javascript: Initialize some property for each object in a list

Created on 24 Oct 2017  路  2Comments  路  Source: airbnb/javascript

I want to do this
this.someList.forEach(item => item.someProperty = 'Some Value');

But i get these
no-return-assign Arrow function should not return assignment
no-param-reassign Assignment to property of function parameter 'item'

How do i do it diffrently?

question

All 2 comments

First,

this.someList.forEach(item => { item.someProperty = 'Some Value'; });

since you're not trying to return a value from the arrow function.

As for no-param-reassign, the intent is specifically not to mutate - if item is a plain object, you could do:

const newList = this.someList.map(item => ({ ...item, someProperty: 'Some Value' }));

Yes they are plain objects so ill use map.
Didnt know about the ... syntax.
Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aboyton picture aboyton  路  113Comments

francoisromain picture francoisromain  路  155Comments

josmardias picture josmardias  路  37Comments

taion picture taion  路  35Comments

SimenB picture SimenB  路  34Comments