javascript - ESLint:分配给函数参数的属性
问题描述
给定如下结构:
products: {
'123': {
details: [
{
price: 45,
unitPrice: 0,
productName: 'apples'
}
]
}
}
还有一个功能,
function modifyPriceForApples(coefficient) {
const products = cloneDeep(vehicleProductSelector.getAllProducts(store.getStore().getState()));
let appleProduct;
Object.keys(products).forEach((productId) => {
const {
details
} = vehicleProducts[productId];
details.forEach((detail) => {
const {
price,
productName
} = detail;
if (productName === 'apples') {
detail.unitPrice = coefficient * detail.price;
appleProduct = products[productId];
}
});
});
return appleProduct;
}
我收到 linting 错误:Assignment to property of function parameter
我该如何解决这个问题,除非禁用 linting 规则?
我一直认为数组解构是这个问题的答案,但是我不太确定在实践中会是什么样子,因为这是一个非常复杂的结构。
解决方案
代替 using forEach
,使用 amap
并表现得好像提供给函数的参数是不可变的。你正在做details.forEach((detail) => {...});
然后分配给detail
with很生气detail.unitPrice = coefficient * detail.price;
。
推荐阅读
- python - 如何在python中获取最后发送的消息值
- javascript - HTML 日历禁用特定日期
- java - Hazelcast Jet 与命名的 Jdbc 查询或结果集?
- terraform - 如何使用 terraform 在 aws 中为多个实例创建多个目标组?
- python - 通过 pip 安装本地 cython 包不会使其模块可访问
- cdn - 有没有办法比较从 2 个不同服务器提供的同一图像的响应时间?
- javascript - 如何在反应和表达中一次上传文件和json数据
- shell - 什么是:/usr/sbin/grub-probe: 错误:未能获得“覆盖”的规范路径?
- c# - 如何在 C#.net 中使用操作标签在本地启动 html 文件
- c# - 获取一周中的几天,一个月中的一天