Question
The following code loops through the array of cars and displays all three Makes (Ford, BMW and Fiat) and display all the models for every
The following code loops through the array of cars and displays all three Makes (Ford, BMW and Fiat) and display all the models for every make. URL for W3Schools code is - https://www.w3schools.com/js/tryit.asp?filename=tryjson_array_nested
Modify the Code to only loop through Fiat Make and Display only the two Fiat Models.
Update the Code below and insert the modified code to limit retrieval only for Fiat.
var myObj, i, j, x = "";
myObj = {
"name":"John",
"age":30,
"cars": [
{"name":"Ford", "models":["Fiesta", "Focus", "Mustang"]},
{"name":"BMW", "models":["320", "X3", "X5"]},
{"name":"Fiat", "models":["500", "Panda"] }
]
}
for (i in myObj.cars) {
x += "
" + myObj.cars[i].name + "
";for (j in myObj.cars[i].models) {
x += myObj.cars[i].models[j] + " ";
}
}
document.getElementById("demo").innerHTML = x;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started