I want to count the objects in an array, I have this code which does not work in HTML. It has to be shown in HTML.
I only want it to show one of the object at time:
Type:
Chocolate: 1, Fruit: 2, Food: 3, Liquid: 2
//This is the variable, i need to count
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],
},
]
var count = []
for (var i = 0; i < markers.length; i++) {
count[markers[i].type] = count[markers[i].type] + 1 || 1;
}
document.getElementById('data').innerHTML = count;
<div id='data'></div>
It has to look like this
Chocolate: 1, Fruit: 2, Food: 3, Liquid: 2
I hope somebody can help me, it has to be vanilla javascript