首页 > 解决方案 > 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 规则?

我一直认为数组解构是这个问题的答案,但是我不太确定在实践中会是什么样子,因为这是一个非常复杂的结构。

标签: javascriptarrayseslint

解决方案


代替 using forEach,使用 amap并表现得好像提供给函数的参数是不可变的。你正在做details.forEach((detail) => {...});然后分配给detailwith很生气detail.unitPrice = coefficient * detail.price;


推荐阅读