Referencing a javascript object literal array

后端 未结 5 1867
旧时难觅i
旧时难觅i 2021-02-08 22:57

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...th

5条回答
  •  情书的邮戳
    2021-02-08 23:39

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

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

提交回复
热议问题