Reputation: 61
I have a json file converted into a js object
var factory = {
city: "turin",
street: "corso unione sovietica",
nAddres: 74,
operative: true,
models: ["toyota", "chevrolet", "ford", "subaru", "honda"],
workspace: {
offices: 12,
minsOfPause: 15,
},
cars: {
toyota: {
id: 1,
numberPlate: "S4IIPLE",
broken: false,
insurance: null,
previousOwners: ["Mark", "Sebastian", "Carlos"],
infos: {
parketAt: "425 2nd Street",
city: "San Francisco",
state: "CA",
postalCode: 94107,
},
},
chevrolet: {
id: 2,
numberPlate: "S2IALR",
broken: true,
insurance: null,
previousOwners: ["Robert", "Mark"],
infos: {
parketAt: "711-2880 Nulla St",
city: "Mankato",
state: "MS",
postalCode: 96522,
},
},
},
};
var json = JSON.stringify(factory);
json = JSON.parse(json);
I have to display the data in a ul li list in an HTML file but when I try to iterate the objects with
for(var a in json){
console.log(a);
}
the console says that the object is not iterable and with
for(var a of json){
console.log(a);
the console doesn't display the value pls help
Upvotes: 0
Views: 98
Reputation: 291
A for...of
loop is used to loop through an array.
For an object, we should use for...in
loop.
Here, it should be :
for (let a in json){
console.log(a);
}
Upvotes: 1