admin管理员组文章数量:1356071
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
)
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.
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.
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
)
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.
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.
Share Improve this question edited Feb 7, 2017 at 13:57 Maytham Fahmi 33.5k16 gold badges127 silver badges153 bronze badges asked Feb 7, 2017 at 13:52 WellNoWellNo 6593 gold badges15 silver badges36 bronze badges 3-
Have you checked if
quantitys
andprices
are as you expect them to be? – elementzero23 Commented Feb 7, 2017 at 13:54 - Yeah I did and they exactly like in the question – WellNo Commented Feb 7, 2017 at 14:00
-
I can only assume that you're getting syntax errors with your variable assignments (
Uncaught SyntaxError: Unexpected identifier
) since neither of those assignments declare, or initialise, an Array. To do so they should look like:price = [14.60, 39.00];
Note the surrounding square brackets. – David Thomas Commented Feb 7, 2017 at 14:00
6 Answers
Reset to default 3You are using two loops while you should only use one. Also, add to endPrice
by using +=
:
var endPrice = 0;
for (var q = 0; q < quantitys.length; q++) {
endPrice += parseFloat(quantitys[q]) * parseFloat(prices[q]);
}
alert(endPrice);
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 = ...
. This will override the endPrice
every time you go through this line. You need to use +=
that will add to the current 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. (See @David Thomas's answer)
You can use 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);
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];
}
To multiply every price in one Array by the number held at the same index in a second Array I'd remend:
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 ments:
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);
本文标签: javascriptAdd multiple numbers to ArrayStack Overflow
版权声明:本文标题:javascript - Add multiple numbers to Array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744050604a2582352.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论