如何将一组数字连接成一个总数?
How to concatenate an array of number into one total number?
我正在尝试获取任意长度的整数数组,并将其连接成一个数字,即总和。例如,如果我有一个如下所示的数组:[2, 2] 我希望它变成 [4]。
我目前正在使用 for 循环在复选框上使用 .push() 生成数组,并要求总计将其添加到另一个方程式。
我正在尝试为价格数组执行此操作:
for(var i=0; i < toppings.length; i++){ // CREATES LOOP FOR EXTRA DATA
if(toppings[i].checked) { //IF CHECKED
storeExtNames += products[productsList.selectedIndex].extra[i].name + " ";
storeExtPrice.push(products[productsList.selectedIndex].extra[i].price);
}//END IF
}//END LOOP
您可以像这样遍历数组:
var arr = [ 1, 2, 3, 4, 5, 6 ];
while (arr.length > 1) {
arr[0] += arr.pop();
}
我正在尝试获取任意长度的整数数组,并将其连接成一个数字,即总和。例如,如果我有一个如下所示的数组:[2, 2] 我希望它变成 [4]。
我目前正在使用 for 循环在复选框上使用 .push() 生成数组,并要求总计将其添加到另一个方程式。
我正在尝试为价格数组执行此操作:
for(var i=0; i < toppings.length; i++){ // CREATES LOOP FOR EXTRA DATA
if(toppings[i].checked) { //IF CHECKED
storeExtNames += products[productsList.selectedIndex].extra[i].name + " ";
storeExtPrice.push(products[productsList.selectedIndex].extra[i].price);
}//END IF
}//END LOOP
您可以像这样遍历数组:
var arr = [ 1, 2, 3, 4, 5, 6 ];
while (arr.length > 1) {
arr[0] += arr.pop();
}