I need to find the end of a loop or the length of the object. Some context on the code:
I obtain the txt
var from the DOM (that's the only way I've got by now and can't change it) and it's a formatted JSON. Here's an example of the expected JSON output:
{"SE DIO AVISO A CENTRAL":true,"OTRO":false,"SE DIO AVISO A SEGURIDAD CIUDADANA":true,"SIN ACCIÓN":false,"SE DIO AVISO A AMBULANCIA":false,"SE DIO AVISO A CARABINEROS":true,"SE DIO AVISO A BOMBEROS":false}
My idea here is to only show the ones with value true
one after the other followed by a comma, but changing that at the end of the loop for a dot. For the moment I've got the commas down but not the dot, which is why I need to know either the length of obj
or the end of the $.each()
loop. Any improvements on overall code are appreciated.
try{
var obj=jQuery.parseJSON(txt);
$(self).text(" ");
var i = 0;
jQuery.each(obj, function(text, val){
if(val){
if(i == 0){
$(self).append(text);
}else{
$(self).append(", "+text);
}
}
i++;
});
}
catch(e){
}