I am trying to loop through array value in JavaScript using a for loop. The for loop also outputs the values onto the page.
I have done this successfully, but there seems to be an error as nothing is being put onto the page, but there are no console errors when I check.
var cars = {
0: {
make: ["Mazda"],
type: ["6 Saloon"],
colour: ["Red"],
topspeed: ["131mph"],
oto60: ["10.0s"],
price: ["£18,282"],
condition: ["Good"],
year: ["2018"]
},
1: {
make: ["Ford"],
type: ["Mondeo Sedan"],
colour: ["Beige"],
topspeed: ["116mph"],
oto60: ["11.0s"],
price: ["£22,900"],
condition: ["Clean"],
year: ["2016"]
},
2: {
make: ["Kia"],
type: ["Rio"],
colour: ["White"],
topspeed: ["106mph"],
oto60: ["12.6s"],
price: ["£3,900"],
condition: ["Average"],
year: ["2004"]
},
3: {
make: ["Audi"],
type: ["A4"],
colour: ["Black"],
topspeed: ["149mph"],
oto60: ["7.6s"],
price: ["£15,300"],
condition: ["Excellent"],
year: ["2015"]
},
4: {
make: ["Mercedes Benz"],
type: ["C Class"],
colour: ["Red"],
topspeed: ["148mph"],
oto60: ["6.7s"],
price: ["£15,462"],
condition: ["Good"],
year: ["2016"]
},
5: {
make: ["BMW"],
type: ["1 Series"],
colour: ["Grey"],
topspeed: ["124mph"],
oto60: ["10.3s"],
price: ["£12,293"],
condition: ["Clean"],
year: ["2015"]
}
};
var i;
for (i = 0; i < cars.length; i++) {
document.getElementById("demo").innerHTML = cars[i]["type"];
}
<p id="demo"></p>