简体   繁体   English

向Array添加多个数字

[英]Add multiple numbers to Array

I have two arrays. 我有两个数组。 They look like this: 它们看起来像这样:

array price = 14.60, 39.00

and

array quantity = 10, 5

(quantity is the quantity of items the user want to buy - 10 items from productA and 5 of productB ) (数量为项目的用户想购买的数量-从10项productA和5 productB

Now I want loop through the variables to multiply the price with the quantity. 现在我想通过变量循环来将价格乘以数量。

Like : 喜欢 :

14,60 * 10

and

39,00 * 5

and add the two results to the endPrice variable. 并将两个结果添加到endPrice变量。

I get the quantity array like this: 我得到这样的数量数组:

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

and the different prices like this: 和这样的不同价格:

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

And that's what I tried: 这就是我尝试过的:

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);

Well, that haven't worked for me so well. 嗯,这对我来说并没有那么好用。 Can someone help me there? 有人可以帮助我吗? Doesn't matter if the solution is pure JavaScript or jQuery. 如果解决方案是纯JavaScript或jQuery无关紧要。

You are using two loops while you should only use one. 您使用两个循环,而您应该只使用一个循环。 Also, add to endPrice by using += : 另外,使用+=添加到endPrice

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

alert(endPrice);

You can't use the double loop for this. 你不能使用双循环。 This multiplies every price with every quantity. 这会使每个价格与每个数量相乘。 What you want to do is this: 你想要做的是:

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

1st problem 第一个问题

You were using nested loops thus every quantity would be multiplied by every prices. 您使用的是嵌套循环,因此每个数量都会乘以每个价格。 You only need one loop. 你只需要一个循环。

2nd problem 第二个问题

You were using endPrice = ... . 您使用的是endPrice = ... This will override the endPrice every time you go through this line. 每次通过此行时,这将覆盖endPrice You need to use += that will add to the current enbPrice 您需要使用+=将添加到当前的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); 

EDIT 编辑

OP need to have separated totals. OP需要分开总计。 (See @David Thomas's answer) (见@David Thomas的回答)

You can use Array.prototype.map() 你可以使用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); 

To multiply every price in one Array by the number held at the same index in a second Array I'd recommend: 要将一个数组中的每个价格乘以第二个数组中相同索引处的数字,我建议:

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"

Close, you're missing += instead of = 关闭,你缺少+=而不是=

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

Dependent on your values, you may also want to parse them, so: 根据您的值,您可能还需要解析它们,因此:

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

Edit with more information in comments: 编辑注释中的更多信息:

Because of the way your code is, they prices are on the same row as the quantities, so they'll be the same. 由于代码的方式,它们的价格与数量在同一行,因此它们将是相同的。 So, the new code will be... 所以,新代码将是......

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