I want to sum all floats in an array
when I do this in the chrome console
$("tr.project_hours").find("td.number").map(function(i,v)
{return parseFloat($(v).attr("data-time"))
})
I get
[0, 0, 0, 0, 0, 0, 0]
This is what I want
But, in my code I have more $(“tr.project_hours”) and I want to sum them separately.
So, I do
$("tr.project_hours").each(function(){
row = $(this).find('td.total')
row.html(sumInputsIn($(this).find("td.number").map(function(i,v){
return parseFloat($(v).attr("data-time"))})));
})
(‘td.total’) is the column where the result should be displayed. The problem is that the last code returns this in the console
[0, 0, 0, 0, 0, 0, 0, prevObject: jQuery.fn.jQuery.init[7], context: <tr>]
So the result is the notorious NaN.
Why do I get the prevObject in my array? And how can I refactor to get rid of it?
This should carry out the expected result:
jQuery map performs
for (item in array)which apparently does not filter byhasOwnProperty()if you are getting those keys. In this case you need to write the logic yourself I guess.