引用javascript对象文字数组

时间:2021-02-12 21:21:16

How would you reference the models (Accord, CRV, Prius, etc) in this structure? Is this a bad structure to be able to extract the makes...then use a make to get the models...then use the model to get the options?

你会如何在这个结构中引用模型(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"] }
        ]
    }
];              

Thanks

5 个解决方案

#1


7  

The structure:

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'] }
        ]}
];

I wrote about the traversal and everything else here.

我在这里写了关于遍历和其他一切的文章。

#2


6  

cars[0].models.Accord cars[0].models.CRV cars[0].models.Pilot (See olliej's answer)

汽车[0] .models.Accord汽车[0] .models.CRV汽车[0] .models.Pilot(见olliej的回答)

Though, it may be easier to use the following access concept:

但是,使用以下访问概念可能更容易:

cars.Honda.Accord
cars.Toyota.Prius

...using...

var cars = {
  Honda : {
    Accord : ["2dr", "4dr"],
    CRV    : ["2dr", "Hatchback"],
    Pilot  : ["base", "superDuper"]
  },
  Toyota : {
    Prius : ["green", "reallyGreen"],
    Camry : ["sporty", "square"],
    Corolla : ["cheap", "superFly"]
  }
};

#3


3  

Jonathan's is correct, but he missed the additional level of Array's at the model level, so it should be

Jonathan是正确的,但他在模型级错过了额外的Array级别,所以它应该是

 cars[0].models[0].Accord
 cars[0].models[1].CRV

etc

I suspect you would find it easier to use a structure along the lines of:

我怀疑你会发现使用以下结构的结构更容易:

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"]
 }
}];

In which the models array is replaced by an object (or associative array if you like)

其中models数组被一个对象(或者你喜欢的关联数组)替换

[edit (olliej): tidying up code in second example]

[编辑(olliej):在第二个例子中整理代码]

#4


2  

You can traverse models with this code:

您可以使用以下代码遍历模型:

for (var i = 0, carslen = cars.length; i < carslen; i++) {
    for (var j = 0, modelslen = cars[i].models.length; j < modelslen; j++) {
        // do something with cars[i].models[j]
    }
}

but I agree with Olliej about changing the structure of your JSON to his format.

但我同意Olliej关于将JSON的结构更改为他的格式。

#5


0  

If I were you, I wouldn't lump all your data into one big multidimensional array/object literal mess like that. I'd encapsulate each object and use methods to access the data. It'll mess with your brain a lot less.

如果我是你,我不会把你所有的数据都归结为一个大的多维数组/对象文字混乱。我封装了每个对象并使用方法来访问数据。它会让你的大脑更加糟糕。

#1


7  

The structure:

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'] }
        ]}
];

I wrote about the traversal and everything else here.

我在这里写了关于遍历和其他一切的文章。

#2


6  

cars[0].models.Accord cars[0].models.CRV cars[0].models.Pilot (See olliej's answer)

汽车[0] .models.Accord汽车[0] .models.CRV汽车[0] .models.Pilot(见olliej的回答)

Though, it may be easier to use the following access concept:

但是,使用以下访问概念可能更容易:

cars.Honda.Accord
cars.Toyota.Prius

...using...

var cars = {
  Honda : {
    Accord : ["2dr", "4dr"],
    CRV    : ["2dr", "Hatchback"],
    Pilot  : ["base", "superDuper"]
  },
  Toyota : {
    Prius : ["green", "reallyGreen"],
    Camry : ["sporty", "square"],
    Corolla : ["cheap", "superFly"]
  }
};

#3


3  

Jonathan's is correct, but he missed the additional level of Array's at the model level, so it should be

Jonathan是正确的,但他在模型级错过了额外的Array级别,所以它应该是

 cars[0].models[0].Accord
 cars[0].models[1].CRV

etc

I suspect you would find it easier to use a structure along the lines of:

我怀疑你会发现使用以下结构的结构更容易:

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"]
 }
}];

In which the models array is replaced by an object (or associative array if you like)

其中models数组被一个对象(或者你喜欢的关联数组)替换

[edit (olliej): tidying up code in second example]

[编辑(olliej):在第二个例子中整理代码]

#4


2  

You can traverse models with this code:

您可以使用以下代码遍历模型:

for (var i = 0, carslen = cars.length; i < carslen; i++) {
    for (var j = 0, modelslen = cars[i].models.length; j < modelslen; j++) {
        // do something with cars[i].models[j]
    }
}

but I agree with Olliej about changing the structure of your JSON to his format.

但我同意Olliej关于将JSON的结构更改为他的格式。

#5


0  

If I were you, I wouldn't lump all your data into one big multidimensional array/object literal mess like that. I'd encapsulate each object and use methods to access the data. It'll mess with your brain a lot less.

如果我是你,我不会把你所有的数据都归结为一个大的多维数组/对象文字混乱。我封装了每个对象并使用方法来访问数据。它会让你的大脑更加糟糕。