简体   繁体   English

我可以创建多个级别的嵌套对象吗?

[英]Can I create several levels of nested objects?

Working on a project where I need to create an object containing several data sets that each have their own subsequent data sets. 在需要创建一个包含多个数据集的对象的项目上,每个数据集都有自己的后续数据集。 Not sure if this is excessive but I need to be able to accomplish something along these lines. 不知道这是否过多,但我需要能够按照这些原则完成工作。

Here's an example of what I mean, this doesn't work but it should give an idea: 这是我的意思的示例,这不起作用,但是应该给出一个主意:

var menu = {
    "appetizers": {
        soup: {
            num1: "beef",
            num2: "chicken",
            num3: "vegetarian": {
                v1: "soy",
                v2: "tomato",
            };
        },
        "salad": {
            sal1: "apple",
            sal2: "egg",
        }
    }
};

Yes, look at the object below. 是的,请看下面的物体。 You can do something like that. 你可以做类似的事情。

Only remember that objects are key value pairs and so for that it has to be in the form of: 仅记住对象是键值对,因此必须采用以下形式:

{key1:value1, 
 key2:{valueKey: valueValue}
}

Hence the following works: 因此,以下工作:

 var menu = { "appetizers": { "soup": { "num1": "beef", "num2": "chicken", "num3": "vegetarian", "num4": { "v1": "soy", "v2": "tomato", } }, "salad": { "sal1": "apple", "sal2": "egg", } } }; console.log(menu); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从平面对象数组创建对象的嵌套数组(最多 5 个级别) - Create a nested array of objects (up to 5 levels) from flat array of objects 如何访问嵌套在多个对象和数组中的元素的值? - How can I access the value of elements nested deeply within several objects and arrays? 我如何通过嵌套对象数组创建具有嵌套对象 arrays 的 object - How i can create object with nested arrays of objects by array of nested objects 移除 JavaScript 中嵌套对象的层级 - Removing levels in nested objects in JavaScript 如何创建一个函数来访问嵌套对象的元素,这些对象与数组的元素有多个层次? - How to create a function to access elements of a nested object several levels deep with elements of an array? 如何在其他对象数组的每个值上创建多个数组? - How can I create several arrays on each of the values of other array of objects? 如何将该JSON解析为多个“对象”? - how can I parse this JSON into several 'objects'? 如何在Cookie中添加几个对象 - How can I add several objects in the cookie 我如何在流星中使用铁路由器嵌套关卡? - how i can do nested levels with iron-router in meteor? 如何检查对象嵌套了多少层? - How can I check how many levels deep an object is nested?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM