[英]Should I use for loops in ReactJS? Any other way to change multiple elements at once when an Input changes?
I'm creating a POS system with ReactJS.我正在用 ReactJS 创建一个 POS 系统。 Should I use a for loop to calculate the total amount?
我应该使用 for 循环来计算总量吗? Is there any other way to do this?
有没有其他方法可以做到这一点?
let product = testProducts;
let subtotalEl = document.getElementsByClassName("Subtotal");
let quantity = document.getElementsByClassName("Quantity");
for (var i = 0; i < product.length; i++) {
subtotalEl[i].innerHTML = product[i].UnitPrice * quantity[i].value;
}
map
for iterations.map
进行迭代。 For example:例如:
const sum = array_object
.map((item) => item.quantity)
.reduce((prev, curr) => prev + curr, 1);
As you can see, map
was used to go through the array, and reduce
to add the values.如您所见,
map
用于遍历数组,并使用reduce
来添加值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.