向Array添加多个数字

时间:2017-02-07 13:52:11

标签: javascript jquery arrays

我有两个数组。它们看起来像这样:

array price = 14.60, 39.00

array quantity = 10, 5

(数量是用户想要购买的商品数量 - 来自productA的10件商品和productB中的5件商品)

现在我希望循环变量以将价格乘以数量。

喜欢:

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无关紧要。

6 个答案:

答案 0 :(得分:3)

您正在使用两个循环,而您应该只使用一个循环。另外,使用endPrice

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

alert(endPrice);

答案 1 :(得分:2)

你不能使用双循环。这会将每个价格乘以每个数量。你想要做的是:

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

答案 2 :(得分:2)

第一个问题

您使用的是嵌套循环,因此每个数量都会乘以每个价格。你只需要一个循环。

第二个问题

您使用的是endPrice = ...。每次通过此行时,这将覆盖endPrice。您需要使用将添加到当前+=的{​​{1}}

enbPrice

修改

OP需要分开总计。 (参见@David Thomas的回答)

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

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

答案 3 :(得分:2)

将一个数组中的每个价格乘以我建议的第二个数组中相同索引处的数字:

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"

答案 4 :(得分:1)

关闭,您遗漏了+=而不是=

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];
}

答案 5 :(得分:1)

<div id="myModal" class="modal">

    <!-- Modal content -->
    <div class="modal-content">
        <span class="close">&times;</span>
        <img src="tyedit.jpg" height="1024" width="724" />
            <div id="imagine">
                <span id="clicked">0</span><br/>
                <span id="word">VOTES</span>
            </div>
    </div>

    </div>