你能有一个双重嵌套的对象文字吗
Can you have a doubly nested object literal?
我可以有一个像下面的"components"值那样的双重嵌套对象文字吗(语法正确吗)?
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)
这就是如何从这个数组中获得所需的所有信息(这里是一个jsfiddle):
function printRecipes(recipeList) {
for(var i = 0; i < recipeList.length; i++) { //loop through all recipes
var recipe = recipeList[0], //get current recipe
ingredients = recipe.ingredients; //get all ingredients
console.log("This recipe is named", recipe.name, "and can be accessed via", recipe.url);
console.log("These are the ingredients:");
for(var j = 0; j < ingredients.length; j++) { //loop through all ingredients of current recipe
var ingredient = ingredients[j]; //get current ingredient
console.log("You need", ingredient.amount, ingredient.name + "(s)", "mesured in", ingredient.unit);
}
console.log("Finished recipe", name + "'s", "ingredient list, passing to next recipe!");
}
}
相关文章:
- 如何计算对象文字中的键
- 对象文字方法上的Javascript绑定不起作用
- 可以使用“;创建“;作为JavaScript对象文字中的属性名称
- 这两种不同的创建对象文字的方法有什么区别
- Javascript对象文字,POST问题
- 为什么可以'我们在函数体中为函数对象添加属性,就像在javascript中为对象文字添加属性一样
- 如何使用'这个“;对象文字中的关键字
- JS对象文字中的方法和子方法
- 向JavaScript对象文字添加数据
- Javascript对象文字,如何使用“this”来引用对象中的变量
- 如何将xml文件转换为javascript中的对象文字
- 什么's对象文字中带引号和不带引号的键之间的区别
- 使用窗口对象练习我的对象文字函数
- 对象文字数据表中的 Google 可视化样式角色
- 如何创建作用域的动态、基于实例的对象文字
- Javascript将字符串中的对象转换为对象文字
- 我需要一些帮助来理解JavaScript中的对象文字函数
- 为什么动画设置加载在TweenMax中的两个单独的对象文字中
- 对象文字语法怪异
- 对象文字中的类