How to handle eslint no-param-reassign rule in Array.prototype.reduce() functions

前端 未结 3 1684
深忆病人
深忆病人 2021-02-03 18:14

I\'ve recently added the eslint rule no-param-reassign.

However, when I use reduce to build out an object (empty object as initialValue), I find myself need

3条回答
  •  天命终不由人
    2021-02-03 18:49

    One solution would be to leverage the object spread operator

    const newObject = ['a', 'b', 'c'].reduce((result, item, index) => ({
      ...result,
      [item]: index, 
    }), {});
    

提交回复
热议问题