Jay*_*ett 7 javascript data-structures
你会如何在这个结构中引用模型(Accord,CRV,Prius等)?这是一个糟糕的结构,能够提取制作...然后使用make来获取模型...然后使用模型来获取选项?
var cars = [
{
"makes" : "Honda",
"models" : [
{'Accord' : ["2dr","4dr"]} ,
{'CRV' : ["2dr","Hatchback"]} ,
{'Pilot' : ["base","superDuper"] }
]
},
{
"makes" : "Toyota",
"models" : [
{'Prius' : ["green","reallyGreen"]} ,
{'Camry' : ["sporty","square"]} ,
{'Corolla' : ["cheap","superFly"] }
]
}
];
Run Code Online (Sandbox Code Playgroud)
谢谢
结构:
var cars = [
{ name: 'Honda', models: [
{ name: 'Accord', features: ['2dr', '4dr'] },
{ name: 'CRV', features: ['2dr', 'Hatchback'] },
{ name: 'Pilot', features: ['base', 'superDuper'] }
]},
{ name: 'Toyota', models: [
{ name: 'Prius', features: ['green', 'superGreen'] },
{ name: 'Camry', features: ['sporty', 'square'] },
{ name: 'Corolla', features: ['cheap', 'superFly'] }
]}
];
Run Code Online (Sandbox Code Playgroud)
我在这里写了关于遍历和其他一切的文章.
汽车[0] .models.Accord汽车[0] .models.CRV汽车[0] .models.Pilot(见olliej的回答)
但是,使用以下访问概念可能更容易:
cars.Honda.Accord
cars.Toyota.Prius
Run Code Online (Sandbox Code Playgroud)
使用......
var cars = {
Honda : {
Accord : ["2dr", "4dr"],
CRV : ["2dr", "Hatchback"],
Pilot : ["base", "superDuper"]
},
Toyota : {
Prius : ["green", "reallyGreen"],
Camry : ["sporty", "square"],
Corolla : ["cheap", "superFly"]
}
};
Run Code Online (Sandbox Code Playgroud)