英文:
How to save value generated by for loop inside an array
问题
我有以下的代码:
function getNumberWithCommas(number) {
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
}
var price = [
6599, 7659, 899, 569, 899, 279, 219, 1797, 3999, 2769, 599, 1349, 2149, 219,
219, 519, 2499, 2949, 5149, 7689, 15999,
];
for (let i = 0; i < 20; i++) {
var productPrice = document.createElement("span");
productPrice.className = "price";
productPrice.setAttribute("id", `price-id-${i}`);
var calculatedPrice = price[i] * quantities[i];
productPrice.textContent = `R$ ${getNumberWithCommas(calculatedPrice)},00`;
precoTotalDiv.appendChild(productPrice);
var totalPrice = [];
totalPrice.push(calculatedPrice);
console.log(totalPrice);
}
它根据购物车中产品的数量和价格动态创建产品的价格。我想将这些值(calculatedPrice)保存在一个数组中,以便在购物车底部获取总价格(所有价格的总和)。
它将calculatedPrice添加到totalPrice[],但只添加一个值,数组的长度仍然为1。一旦我点击另一个产品,calculatedPrice就会被另一个产品的价格覆盖。我想获得生成的所有价格的总和,而不仅仅是一个产品的总和。
英文:
I have the following code
function getNumberWithCommas(number) {
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
}
var price = [
6599, 7659, 899, 569, 899, 279, 219, 1797, 3999, 2769, 599, 1349, 2149, 219,
219, 519, 2499, 2949, 5149, 7689, 15999,
];
for (let i = 0; i < 20; i++) {
var productPrice = document.createElement("span");
productPrice.className = "price";
productPrice.setAttribute("id", `price-id-${i}`);
var calculatedPrice = price[i] * quantities[i];
productPrice.textContent = `R$ ${getNumberWithCommas(calculatedPrice)},00`;
precoTotalDiv.appendChild(productPrice);
var totalPrice = [];
totalPrice.push(calculatedPrice);
console.log(totalPrice);
}
It dynamically creates prices for products based on their quantity in the cart and their price. I would like to save these values (calculatedPrice) inside an array so I can get a total price (sum of all the prices) at the bottom of the cart.
It is adding calculatedPrice to totalPrice[], but it only adds one value, the length of the array remains one. Once I click another product, calculatedPrice gets overwritten with another products price.
I would like to get a sum of all the prices generated, not the sum of only one product.
答案1
得分: 1
这是因为你在每次循环中都创建了一个新数组。
在循环外部首先声明你的数组:
var totalPrice = [];
for (let i = 0; i < 20; i++) {
var productPrice = document.createElement("span");
productPrice.className = "price";
productPrice.setAttribute("id", `price-id-${i}`);
var calculatedPrice = price[i] * quantities[i];
productPrice.textContent = `R$ ${getNumberWithCommas(calculatedPrice)},00`;
precoTotalDiv.appendChild(productPrice);
totalPrice.push(calculatedPrice);
console.log(totalPrice);
}
英文:
This is because you're creating a new array on each loop
Declare your array outside of the loop first:
var totalPrice = [];
for (let i = 0; i < 20; i++) {
var productPrice = document.createElement("span");
productPrice.className = "price";
productPrice.setAttribute("id", `price-id-${i}`);
var calculatedPrice = price[i] * quantities[i];
productPrice.textContent = `R$ ${getNumberWithCommas(calculatedPrice)},00`;
precoTotalDiv.appendChild(productPrice);
totalPrice.push(calculatedPrice);
console.log(totalPrice);
}
答案2
得分: 1
你可以使用JavaScript中数组的push方法,将for循环生成的值保存在数组中。以下是一个示例:
// 初始化一个空数组
var result = [];
// 使用for循环生成值
for (var i = 0; i < 5; i++) {
// 将生成的值添加到数组中
result.push(i);
}
// 打印最终的数组
console.log(result);
这将输出:
[0, 1, 2, 3, 4]
英文:
You can save the values generated by a for loop inside an array in JavaScript by using the push method of an array. Here's an example:
// Initialize an empty array
var result = [];
// Use a for loop to generate values
for (var i = 0; i < 5; i++) {
// Push the generated value to the array
result.push(i);
}
// Print the final array
console.log(result);
This will output:
[0, 1, 2, 3, 4]
答案3
得分: 1
将 let totalPrice = []
移到循环外部:
因为当你将 totalPrice
放在循环内部时,它会在每次循环时被重置值。
var totalPrice = [];
for (let i = 0; i < 20; i++) {
var productPrice = document.createElement("span");
productPrice.className = "price";
productPrice.setAttribute("id", `price-id-${i}`);
var calculatedPrice = price[i] * quantities[i];
productPrice.textContent = `R$ ${getNumberWithCommas(calculatedPrice)},00`;
precoTotalDiv.appendChild(productPrice);
totalPrice.push(calculatedPrice);
}
console.log(totalPrice);
英文:
just move let totalPrice = []
to outside of the loop:
Because when you put totalPrice inside the loop and it will be reset value on each loop
var totalPrice = [];
for (let i = 0; i < 20; i++) {
var productPrice = document.createElement("span");
productPrice.className = "price";
productPrice.setAttribute("id", `price-id-${i}`);
var calculatedPrice = price[i] * quantities[i];
productPrice.textContent = `R$ ${getNumberWithCommas(calculatedPrice)},00`;
precoTotalDiv.appendChild(productPrice);
totalPrice.push(calculatedPrice);
}
console.log(totalPrice);
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论