javascript - Prefer destructuring es-lint error - Stack Overflow

I have this function:const calculateTotal = (items) => {return items.reduce((totalPrice, basketItem)

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?

I know I need something like (on line 4):

const { basketItem: quantity } = quantity;

but I can't get line 3 working

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?

I know I need something like (on line 4):

const { basketItem: quantity } = quantity;

but I can't get line 3 working

Share Improve this question edited Jan 9, 2018 at 15:55 codejockie 10.9k4 gold badges48 silver badges57 bronze badges asked Jan 9, 2018 at 14:52 The WalrusThe Walrus 1,2087 gold badges31 silver badges50 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 7

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.

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

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

below this destructuring method :

const {quantity}=basketItem;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745188516a4615751.html

相关推荐

  • javascript - Prefer destructuring es-lint error - Stack Overflow

    I have this function:const calculateTotal = (items) => {return items.reduce((totalPrice, basketItem)

    10小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信