簡體   English   中英

向Array添加多個數字

[英]Add multiple numbers to Array

我有兩個數組。 它們看起來像這樣:

array price = 14.60, 39.00

array quantity = 10, 5

(數量為項目的用戶想購買的數量-從10項productA和5 productB

現在我想通過變量循環來將價格乘以數量。

喜歡 :

14,60 * 10

39,00 * 5

並將兩個結果添加到endPrice變量。

我得到這樣的數量數組:

$('.quantity').change(function () {
    quantitys = $('input[type=number]').map(function () {
        return $(this).val();
    }).get();
});

和這樣的不同價格:

var prices = $('.priceCell').map(function () {
    return parseFloat($(this).html().trim());
}).get();

這就是我嘗試過的:

var endPrice = 0;
for (var q = 0; q < quantitys.length; q++) {
    for (var p = 0; p < prices.length; p++) {
    endPrice = quantitys[q] * prices[p];
    }
}

alert(endPrice);

嗯,這對我來說並沒有那么好用。 有人可以幫助我嗎? 如果解決方案是純JavaScript或jQuery無關緊要。

您使用兩個循環,而您應該只使用一個循環。 另外,使用+=添加到endPrice

var endPrice = 0;
for (var q = 0; q < quantitys.length; q++) {
    endPrice += parseFloat(quantitys[q]) * parseFloat(prices[q]);
}

alert(endPrice);

你不能使用雙循環。 這會使每個價格與每個數量相乘。 你想要做的是:

var endPrice = 0;
for (var i = 0; i < quantitys.length; i++) {
    endPrice += quantitys[i] * prices[i];
}

第一個問題

您使用的是嵌套循環,因此每個數量都會乘以每個價格。 你只需要一個循環。

第二個問題

您使用的是endPrice = ... 每次通過此行時,這將覆蓋endPrice 您需要使用+=將添加到當前的enbPrice

 var prices = [14.60, 39.00]; var quantities = [10,5]; var endPrice = 0; for(let i=0, l=prices.length;i<l;i++){ endPrice += prices[i] * quantities[i]; } console.log(endPrice); 

編輯

OP需要分開總計。 (見@David Thomas的回答)

你可以使用Array.prototype.map()

 var prices = [14.60, 39.00]; var quantities = [10, 5]; var totals = prices.map((p, index) => p * quantities[index]); console.log(totals); 

要將一個數組中的每個價格乘以第二個數組中相同索引處的數字,我建議:

var price = [14.60, 39.00],
    quantity = [10, 5],

    // here we iterate over the price Array, using
    // Array.prototype.map() in order to return a
    // new Array:
    totals = price.map(

        // p: the current array-element of
        //    the Array of prices,
        // index: the index of the current
        // array-element of the Array.

        // using an Arrow function to
        // multiply the price ('p') by
        // the value held in the quantity
        // Array at the same index:
        (p,index) => p * quantity[index]
    );

// logging the created 'totals' Array to the console:
console.log(totals); // [146,195]

// logging the created Array, after joining its
// elements together to form a String with values
// separated with a ',' character:
console.log(totals.join(',')); // "146,195"

關閉,你缺少+=而不是=

endPrice += quantitys[q] * prices[p];

根據您的值,您可能還需要解析它們,因此:

endPrice += (parseInt(quantitys[q]) * prices[p]) // you're already parsing prices;

編輯注釋中的更多信息:

由於代碼的方式,它們的價格與數量在同一行,因此它們將是相同的。 所以,新代碼將是......

for (var q = 0; q < quantitys.length; q++) {
    endPrice += parseInt(quantitys[q]) * prices[q];
}

 const prices = [14.60, 39.00]; const qty = [10, 5]; const endPrice = prices.reduce((total, price, i) => total + price * qty[i], 0); console.log(endPrice); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM