Javascript nested arrays - Stack Overflow

I am currently developing a website to do with cooking and will store recipes.At the moment I am planni

I am currently developing a website to do with cooking and will store recipes.

At the moment I am planning to store the recipes in a JS nested array. The array will contain all the recipes and then within each of the recipes will be another array containing all the ingredients for that recipe.

What would be the best way to structure this nested array?

Currently I have the following but I'm not entirely sure this is the best/correct way to do it...

Any help is much appreciated.

var recipes = [
    {
        name:"pizza",
        ingredients: {
            "tomato",
            "cheese",
            "meat"
        }
    }
]

I am currently developing a website to do with cooking and will store recipes.

At the moment I am planning to store the recipes in a JS nested array. The array will contain all the recipes and then within each of the recipes will be another array containing all the ingredients for that recipe.

What would be the best way to structure this nested array?

Currently I have the following but I'm not entirely sure this is the best/correct way to do it...

Any help is much appreciated.

var recipes = [
    {
        name:"pizza",
        ingredients: {
            "tomato",
            "cheese",
            "meat"
        }
    }
]
Share Improve this question asked Jul 26, 2013 at 22:37 PhilPhil 1,6624 gold badges26 silver badges41 bronze badges 1
  • I think that's a perfectly fine way, although it should be: ingredients: [..] if an array is desired (it is a syntax error now due to an invalid object literal). Also, what about quantities of ingredients? Might want to expand that out. – user2246674 Commented Jul 26, 2013 at 22:44
Add a ment  | 

2 Answers 2

Reset to default 5

I agree with @smakateer regarding associated array. However I would improve it a bit to:

var recipes = {
    "pizza": { 
        "ingredients": ["tomato", "cheese", "meat" ], //or "ingredients": [ {"name":"tomato", "howMany": 3} ]

        //thanks to this it will be easier extendable, i.e.
        "description": "Some description",
        imageUrl: URL
    }
}

EDIT: You'll probably have many receips for pizza, so you could store them in array of objects under one key.

...
"pizza": [ {...}, {...} ],
"dumplings": []
...

I would suggest moving the name to the top level of the array and switching it to an associated array of strings to arrays:

var recipes = {
    "pizza": [
        "tomato",
        "cheese",
        "meat"
    ]
}

Then you can call each recipe by name and get the iterable list of ingredients.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745636244a4637409.html

相关推荐

  • Javascript nested arrays - Stack Overflow

    I am currently developing a website to do with cooking and will store recipes.At the moment I am planni

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信