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

前端 未结 3 1627
深忆病人
深忆病人 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:36

    I just wrap the reduce functions in a lint rule disable block, ie:

    /* eslint-disable no-param-reassign */
    const newObject = ['a', 'b', 'c'].reduce((result, item, index) => {
      result[item] = index;
      return result;
    }, {});
    /* eslint-enable no-param-reassign */
    

提交回复
热议问题