通过对象数组javascript构建/创建多维对象

时间:2018-11-16 11:07:11

标签: javascript object

我没有Java经验,因此在以下情况下需要帮助:

我在数组中有多个对象,并以此方式遍历它们:

    for each(var obj in objs) {
       if (typeof(obj) === "object" ){
          .
          .
             switch (true) {
                 case  obj.id < 100: levelId=1; break;
                 case  obj.id < 10000: levelId=2; break;
                 case  obj.id < 1000000: levelId=3; break;
                 case  obj.id < 100000000: levelId=4; break;
             }; 
          .
          .  
       }
    }

这种情况是根据ID确定当前对象是否是上一个对象的子对象。

每个对象都具有以下属性:

"Obj": {
    "id": 1,
    "name": "Obj",
    "properties": {
        "attName": "Name",
        "attType": "Type",
        "attOccurance": "maxOccurs="1",
        .
        .
    }

还有更多属性,但我只需要这些。

所以我必须创建一个多维对象,该对象应具有上面对象的正确结构,看起来像这样:

mainObj = {
    "Ob1": {
        "id": 1,
        "name": "Ob1",
        "properties": {
            "attName": "A1",
            "attType": "string",
            "attOccurance": "minOccurs="1""
        },
        "Ob2": {
            "id": 101,
            "name": "Ob2",
            "properties": {
                "attName": "B1",
                "attType": "string",
                "attOccurance": "minOccurs="1""
                },
            "Ob3": {
                "id": 10001,
                "name": "Ob3",
                "properties": {
                    "attName": "C1",
                    "attType": "string",
                    "attOccurance": "minOccurs="1""
                    },
            },
            "Ob4": {
                "id": 10002,
                "name": "Ob4",
                "properties": {
                    "attName": "C2",
                    "attType": "string",
                    "attOccurance": "minOccurs="1""
                    },
            },
        },
        "Ob5": {
            "id": 102,
            "name": "Ob5",
            "properties": {
                "attName": "B2",
                "attType": "string",
                "attOccurance": "minOccurs="1""
            },
        },
    }
    "Ob6": {
        "id": 2,
        "name": "Ob6",
        "properties": {
            "attName": "A2",
            "attType": "string",
            "attOccurance": "minOccurs="1""
        },
    },
}

请帮助您提出想法或一些与此相关的代码

0 个答案:

没有答案