简体   繁体   English

更喜欢解构es-lint错误

[英]Prefer destructuring es-lint error

I have this function: 我有这个功能:

const calculateTotal = (items) => {
  return items.reduce((totalPrice, basketItem) => {
    const price = basketItem.product.price;
    const quantity = basketItem.quantity;
    const total = price * quantity;
    return totalPrice + total;
  }, 0);
};

How do I fix this with ES6+ destructuring? 我如何通过ES6 +解构来解决这个问题?

I know I need something like (on line 4): 我知道我需要类似的东西(第4行):

const { basketItem: quantity } = quantity;

but I can't get line 3 working 但是我不能让第3行工作

const quantity=basketItem.quantity;

在这种解构方法之下:

const {quantity}=basketItem;

Based on the what you attempted doing, you could do this to get price from product and quantity from basketItem without having to declare variables on two separate lines. 根据您尝试的操作,您可以执行此操作以从basketItem获取productquantity price ,而无需在两个单独的行上声明变量。

const calculateTotal = (items) => {
  return items.reduce((totalPrice, basketItem) => {
    const { product: { price }, quantity } = basketItem;

    const total = price * quantity;
    return totalPrice + total;
  }, 0);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM