I’m trying to get the “sum” from a variable.
I have this code now:
function UpdateBuddyStat()
{
$.getJSON('/update-buddies', function(jsonData){
var counterjson = 0;
var LI_list_html = '';
$.each(jsonData, function(i,value){
var count = counterjson++;
var username = jsonData[i].name;
var buddy = jsonData[i].buddyid;
var onoff = jsonData[i].onoffstate;
var DIV_html = username+' - ['+onoff+']<div class="FunctionBuddyBox"><span class="TagBuddy" id="TagsBuddy_'+buddy+'" data-tagbuddyid="'+buddy+'">tagit</span></div>';
LI_list_html = LI_list_html+'<li>'+DIV_html+'</li>';
});
$("#ShowBuddyList ul li").remove();
$("#ShowBuddyList ul").html(LI_list_html);
})
}
What i get is this one:
User1 - [1]
User2 - [0]
User3 - [0]
User4 - [1]
User5 - [0]
So finally i need to sum the var “onoff”. In this case the result would be 2.
I have tried many options but still have’nt luck by this.
Maybe anyone have a suggestion for me? Many thanks in advance!
Just add a
sumcounter and add theonoffvalue for each item (after casting to an integer):