我可以有一個雙重嵌套的對象字面值,如下面的「成分」的值(是正確的語法)?你可以有一個雙重嵌套的對象文字?
recipes = [
{name: 'Zucchini Muffins', url: 'pdfs/recipes/Zucchini Muffins.pdf',
ingredients: [{name: 'carrot', amount: 13, unit: 'oz' },
{name: 'Zucchini', amount: 3, unit: 'sticks'}]
}
];
如果是這樣,我將如何訪問「成分」對象的「單位」值?
我可以這樣做嗎?
僞碼
for each recipes as recipe
print "this recipe requires"
for each recipe.ingredients as ingredients
ingredients.amount + " " + ingredients.unit;
(我想用javascript的)
你可以讓它像那樣構造,但是; '食譜'是_Array_。 'recipes [0]'是一個_Object_。 '食譜[0] .ingredients'是_Array_。 'recipes [0] .ingredients [0]'是另一個_Object_。 '食譜[0] .ingredients [0] .unit'是'「oz」'。另外,['for each..in'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for_each...in)被折舊並且不明智無論如何,在_Array_上使用。 –
爲什麼不嘗試並找出答案? –
@paul啊,好吧,那是我的直覺。感謝每個人的頭像。 @ Matt Burland對語法尚不確定,我想在深入細節之前對概念進行概述。 – LazerSharks