admin管理员组文章数量:1415100
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 02 Answers
Reset to default 7Based 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;
本文标签: javascriptPrefer destructuring eslint errorStack Overflow
版权声明:本文标题:javascript - Prefer destructuring es-lint error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745188516a2646798.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论