I have made this function that counts an object in an array but I want to put it in another function, it has to work in another function I've made. I want it to be vert vanilla java because I am new to programming
var markers = [ { type:"Chocolate", name:"KitKat", group:"candy", icon:"candy", coords:[5246,8980], }, { type:"Fruit", name:"Orange", group:"fruits", icon:"fruis", coords:[9012,5493], }, { type:"Fruit", name:"Banana", group:"fruits", icon:"fruis", coords:[9012,5493], }, { type:"Food", name:"Rice", group:"foods", icon:"foods", coords:[6724,9556], }, { type:"Food", name:"Meat", group:"foods", icon:"foods", coords:[6724,9556], }, { type:"Food", name:"Beam", group:"foods", icon:"foods", coords:[6724,9556], }, { type:"Liquid", name:"Water", group:"liquids", icon:"liquids", coords:[6724,9556], }, { type:"Liquid", name:"Coffe", group:"liquids", icon:"liquids", coords:[6724,9556], }, ]; function counter(){ var count = {} for (var i = 0; i < markers.length; i++) { count[markers[i].type] = count[markers[i].type] + 1 || 1; } document.getElementById('data').innerHTML = JSON.stringify(count); } counter();
this one here
function make(){ var pg = '<table>' for(i = 0;i < markers.length; i++){ pg += '<td>' + ??? + '</td>'; // I would like to put it here og += '<td>' + markers[i].name + '</td>'; og += '</tr>'; } document.getElementById('data').innerHTML = pg; }
I hope somenone can help me, also if we have to change the functions so they can work togehther
You can return the object from that previous function instead of directly rendering it to the DOM.
function counter() {
var count = {}
for (var i = 0; i < markers.length; i++) {
count[markers[i].type] = count[markers[i].type] + 1 || 1;
}
return count;
}
Replace the ???
with JSON.stringify(counter())
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.