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

olalonde picture olalonde  路  3Comments

danielfttorres picture danielfttorres  路  3Comments

kozhevnikov picture kozhevnikov  路  3Comments

tunnckoCore picture tunnckoCore  路  3Comments

xgqfrms-GitHub picture xgqfrms-GitHub  路  3Comments