I am been having trouble counting the number of objects in this array in javascript. Below is the array of objects i try to count with my code.
<script>
var arr = [
{"gateways":["ccu1"],"manufacturer":["homematic"],"ir":["ir_no"],"ip":["ip_cam","ip_other"]},
{"gateways":["v3"],"manufacturer":["homematic"],"ir":["ir_no"],"ip":["ip_cam"]},
{"gateways":["v2","v3","v4","ccu2"],"manufacturer":["homematic","intertechno"],"ir":["ir_yes"],"ip":["ip_cam","ip_other"]},
{"gateways":["v2","ccu1","ccu2"],"manufacturer":["homematic"],"ir":["ir_yes"],"ip":["ip_cam","ip_other"]},
{"gateways":["gw_none"],"manufacturer":["homematic"],"ir":["ir_no"],"ip":["ip_cam"]},
{"gateways":["v3","ccu2"],"manufacturer":["homematic","fs20","intertechno","elro","Eltako Enocean"],"ir":["ir_yes"],"ip":["ip_cam","ip_other"]},
{"gateways":["v3","v4"],"manufacturer":["homematic"],"ir":["ir_no"],"ip":["ip_other"]},
{"gateways":["v3","v4"],"manufacturer":["homematic"],"ir":["ir_no"],"ip":["ip_other"]},
{"gateways":["v2"],"manufacturer":["intertechno"],"ir":["ir_yes"],"ip":["ip_other"]}
];
var counter = [];
for(var i=0; i<arr.length; i++) {
//console.log(arr[i]);
for(var index in arr[i]) {
console.log(index);
if(counter[index] === undefined) {
counter[index] = [];
}
}
}
console.log(counter);
</script>
I want the number of the objects to push into the empty array "counter" when you console log "counter" e.g.
gateways
ccu2 42 v4 70 v2 95 v3 91 v4plus 32 ccu1 16 gw_none 10
ip
ip_cam 4 ip_other 10 ip_none 4
ir
ir_yes 13 ir_no 18
manufacturer
homematic 24 fs20 59 intertechno 38 elro 63 homeeasy 40 somfy 11
I am new to programming and trying my hands on some few exercises like this one but i got stuck. I'm left with the codes to put the object counter to the empty array. I have tried but cannot let it work. I would appreciate any help and i hope my assignment makes sense and is understandable.
Aucun commentaire:
Enregistrer un commentaire