admin管理员组文章数量:1316356
I have this sample code:
<table>
<tr>
<td class="price">1</td>
</tr>
<tr>
<td class="price">4</td>
</tr>
<tr>
<td class="price">6</td>
</tr>
</table>
<p id="setTotal"> </p>
I want to get the total of those values under class "price" however my output goes something like:
1 4 6 Sum is 0[object HTMLTableCellElement][object HTMLTableCellElement][object HTMLTableCellElement].
My JavaScript code is:
var arr = [];
var totalPrice = 0;
var i;
$("td.price").each(function(){
arr.push($(this).text());
totalPrice += this;
document.getElementById("setTotal").innerHTML = "Sum is "+totalPrice+ ".";
});
I have this sample code:
<table>
<tr>
<td class="price">1</td>
</tr>
<tr>
<td class="price">4</td>
</tr>
<tr>
<td class="price">6</td>
</tr>
</table>
<p id="setTotal"> </p>
I want to get the total of those values under class "price" however my output goes something like:
1 4 6 Sum is 0[object HTMLTableCellElement][object HTMLTableCellElement][object HTMLTableCellElement].
My JavaScript code is:
var arr = [];
var totalPrice = 0;
var i;
$("td.price").each(function(){
arr.push($(this).text());
totalPrice += this;
document.getElementById("setTotal").innerHTML = "Sum is "+totalPrice+ ".";
});
Share
Improve this question
edited Sep 21, 2015 at 15:35
indubitablee
8,2162 gold badges27 silver badges49 bronze badges
asked Sep 21, 2015 at 15:26
NorsebackNorseback
3153 gold badges6 silver badges19 bronze badges
1
- Do you need it in array, or string output is ok? You are trying both ways, as i can see? – sinisake Commented Sep 21, 2015 at 15:32
6 Answers
Reset to default 3You need to get the text from the td and parse it as a number.
fiddle:http://jsfiddle/4rwbyx3n/
var arr = [];
var totalPrice = 0;
var i;
$("td.price").each(function(){
arr.push($(this).text());
var price = $(this).text();
totalPrice += Number(price);
document.getElementById("setTotal").innerHTML = "Sum is "+totalPrice+ ".";
});
You have two issues:
- You're incrementing
totalPrice
bythis
, which is an HTML element. - You are not converting the string value from the HTML into an integer.
Here are the changes, with some minor improvements/suggestions:
var totalPrice = 0;
$("td.price").each(function(i, td) {
totalPrice += parseInt($(td).text());
});
$('#setTotal').html("Sum is " + totalPrice + ".");
Try:
$("td.price").each(function(){
arr.push($(this).text());
totalPrice += (+$(this).text());
document.getElementById("setTotal").innerHTML = "Sum is "+totalPrice+ ".";
});
The reason for your earlier result is you were concatenating HTML
elements, not the text in it.
something like this:
With javascript's array.map you can transform an array of something, into something else.
In this case, an array of html elements into an array of numbers.
using reduceRight on the result with a simple add function as a parameter, each element of the array is accumulated and summed one by one.
we need to wrap it in jQuery.makeArray, since jQuery $(selector).map will return a jQuery object, and we want a native javascript array.
var sum = jQuery.makeArray($("td.price").map(function(idx, num) {
return parseInt($(num).text());
}).reduceRight(function(a,b){return a+b;}));
and then
document.getElementById("setTotal").innerHTML = "Sum is "+sum+ ".";
or with jquery
$("#setTotal").text("Sum is " + sum + ".");
you have to parseFloat
the text of the element. and you also need jquery
for what youre doing with the .each()
function
var totalPrice = 0;
$("td.price").each(function(){
totalPrice += parseFloat($(this).text());
document.getElementById("setTotal").innerHTML = "Sum is "+totalPrice+ ".";
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td class="price">1</td>
</tr>
<tr>
<td class="price">4</td>
</tr>
<tr>
<td class="price">6</td>
</tr>
</table>
<p id="setTotal"> </p>
You are pushing html elements/objects to your sum, not sure why, and since you are already using jQuery, no need for native JS selectors and methods:
var totalPrice = 0;
$("td.price").each(function(){
totalPrice +=parseInt($(this).text());
});
$("#setTotal").html("Sum is "+totalPrice+ ".");
Also, you can move price showing from each() loop, no need for updates inside loop...
http://jsfiddle/216fouoy/
本文标签: javascriptGet the sum of values in arrayStack Overflow
版权声明:本文标题:javascript - Get the sum of values in array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742002588a2411316.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论