【问题标题】:Assignment to property of function parameter 'item'分配给函数参数'item'的属性
【发布时间】:2021-12-27 14:15:30
【问题描述】:

我有那个 lint 错误:赋值给函数参数“item”的属性

消除此错误的正确方法是什么?

const resp = await getData(payload)
resp.forEach((item) => {
      item[0] = moment(item[0]).format('DD/MMM/YY');
});

【问题讨论】:

  • 正在分配参数的属性。您可以使用.map 并创建一个新数组的新数组,或者禁用此特定功能的规则。
  • getData 是否返回 JSON?你在哪里解析那个 JSON?另外,每个item 的结构是什么?你真的可以这样设置索引吗?您可能应该使用 map 而不是 forEach 进行此操作。
  • [ [ "07/10/2020", 0.04, 0.05, 0.04 ], [ "07/10/2020", 0.04, 0.06, 0.04 ] ] @Andy 这就是 getData 的回报跨度>
  • 我尝试使用地图但我不能很好地使用它

标签: javascript reactjs eslint lint


【解决方案1】:

这是map 的示例。

const response = [ [ "07/10/2020", 0.04, 0.05, 0.04 ], [ "07/10/2020", 0.04, 0.06, 0.04 ] ];

// Iterate over the response
const data = response.map(item => {

  // For each item destrucuture the data from
  // the rest of the elements
  const [date, ...rest] = item;

  // Return a new array which has the updated
  // date, and the other elements then spread out
  return [
    moment(new Date(date)).format('DD/MMM/YY'),
    ...rest
  ];

});

console.log(data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

其他文档

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    • 1970-01-01
    • 2020-11-20
    • 2011-09-22
    • 1970-01-01
    • 2020-05-10
    • 2013-02-25
    相关资源
    最近更新 更多