javascript - Can you have a doubly nested object literal? -
can have doubly nested object literal value of "ingredients" below (is syntax correct)?
recipes = [ {name: 'zucchini muffins', url: 'pdfs/recipes/zucchini muffins.pdf', ingredients: [{name: 'carrot', amount: 13, unit: 'oz' }, {name: 'zucchini', amount: 3, unit: 'sticks'}] } ];
and if so, how access "unit" value of "ingredients" objects?
could this?
psuedocode
for each recipes recipe print "this recipe requires" each recipe.ingredients ingredients ingredients.amount + " " + ingredients.unit;
(i'm thinking of using javascript)
this how can infos need array(here jsfiddle):
function printrecipes(recipelist) { for(var = 0; < recipelist.length; i++) { //loop through recipes var recipe = recipelist[0], //get current recipe ingredients = recipe.ingredients; //get ingredients console.log("this recipe named", recipe.name, "and can accessed via", recipe.url); console.log("these ingredients:"); for(var j = 0; j < ingredients.length; j++) { //loop through 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 next recipe!"); } }
Comments
Post a Comment