I'm using Jquery post method to get data from php file which returned a a multidimensional array which has productid, name and price in it in the following format
[
{ product_id: 34, product_name: "Coca-Cola", price: 0.7 },
{ product_id: 24, product_name: "Shredded Beef Steak Wrap", price: 2.99 }
]
when i add them those two prices it does not add up together infect they concatenate. this is how i'm trying to add them up
$.each(data, function(i, result) {
//total = total + +result.price;
//total += +result.price;
//total += result.price;
//total += +total + +result.price;
//total = total * 1 + result.price;
});
$("#cart_total").empty();
$("#cart_total").append(output);
Any idea what should i do to get this right
Regards
I'm using Jquery post method to get data from php file which returned a a multidimensional array which has productid, name and price in it in the following format
[
{ product_id: 34, product_name: "Coca-Cola", price: 0.7 },
{ product_id: 24, product_name: "Shredded Beef Steak Wrap", price: 2.99 }
]
when i add them those two prices it does not add up together infect they concatenate. this is how i'm trying to add them up
$.each(data, function(i, result) {
//total = total + +result.price;
//total += +result.price;
//total += result.price;
//total += +total + +result.price;
//total = total * 1 + result.price;
});
$("#cart_total").empty();
$("#cart_total").append(output);
Any idea what should i do to get this right
Regards
Share Improve this question asked Feb 19, 2015 at 11:20 SharifSharif 7281 gold badge7 silver badges17 bronze badges 2- What you want total of price ? – Sadikhasan Commented Feb 19, 2015 at 11:23
-
Before your
$.each
loop you have to initializetotal=0
it work fine. – Sadikhasan Commented Feb 19, 2015 at 11:27
8 Answers
Reset to default 2try this...
var data = [
{ product_id: 34, product_name: "Coca-Cola", price: 0.7 },
{ product_id: 24, product_name: "Shredded Beef Steak Wrap", price: 2.99 }
];
var sum = 0;
data.forEach(function(i){
sum =sum + +i.price;
});
alert(sum);
<script src="https://ajax.googleapis./ajax/libs/jquery/1.10.2/jquery.min.js"></script>
var data = [
{ product_id: 34, product_name: "Coca-Cola", price: 0.7 },
{ product_id: 24, product_name: "Shredded Beef Steak Wrap", price: 2.99 }
];
To add
var sum = 0;
data.forEach(function(i){
sum += +i.price;
});
$("#cart_total").empty();
$("#cart_total").append(sum);
You have to initialize
total
variable before loop started and you can perform your task.
total = 0;
$.each(data, function(i, result) {
total = total + result.price;
});
alert(total);
Demo
User parseInt function :
sum=parseInt(price)+parseInt(price);
initialize total firstly : total = 0; then :
total += parseFloat(result.price);
try this!
Try this:
total = Number(total) + Number(total.price);
Concatenation happens because total
must be a string, even if it "contains" a number. In other words:
'1' === 1
Means false
in javascript.
What we're doing is coercing total
string to a number:
Number('1') === 1
(true
)
var data = [
{ product_id: 34, product_name: "Coca-Cola", price: 0.7 },
{ product_id: 24, product_name: "Shredded Beef Steak Wrap", price: 2.99 }
];
Then calcultate sum :
var total = 0;
$(data).each(function(index,item) {
total += item.price;
});
total = parseFloat(total.toFixed(2))
Display :
$("#result").html(total);
Working exemple here : http://jsfiddle/9jpdLkp2/
try this
var total = 0;
$(data).each(function(index,item) {
total += item.price;
});
total = parseFloat(total.toFixed(2));
toFixed(2)
is to restrict two digits after decimal
hope that helps