Prefer destruction 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 to fix this with the restructuring of ES6 +?

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

const { basketItem: quantity } = quantity;

but i cant get line 3 to work

+5
source share
2 answers

According to what you did, you can do this to get price from product and quantity from basketItem without declaring 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); }; 
+6
source
 const quantity=basketItem.quantity; 

Below this destruction method:

 const {quantity}=basketItem; 
+6
source

Source: https://habr.com/ru/post/1274597/


All Articles