首页 > 解决方案 > 在javascript中显示多维对象数组的结果

问题描述

我在Javascript方面没有太多经验,所以我向你寻求帮助。我有这样表示的多维对象数组:

arr = {
    "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": 10003,
                "name": "Ob6",
                "properties": {
                    "attName": "C3",
                    "attType": "string",
                    "attOccurance": "minOccurs="1""
                },
            },
        },
    }
    "Ob7": {
        "id": 2,
        "name": "Ob7",
        "properties": {
            "attName": "A2",
            "attType": "string",
            "attOccurance": "minOccurs="1""
        },
    },
}

我如何递归循环遍历这个数组并显示结果,同时跟踪父对象,以防我必须返回 2 个级别,例如在这种情况下使用“Ob7”?

结果必须以 xml 模式方式显示(如果对象有子对象是复杂类型,如果没有,则简单类型:

<xs:complexType name="A1" type="string" minOccurs="1">
   <xs:complexType name="B1" type="string" minOccurs="1">
      <xs:simpleType name="C1" type="string" minOccurs="1"/>
      <xs:simpleType name="C2" type="string" minOccurs="1"/>
   </complexType>
   <xs:complexType name="B2" type="string" minOccurs="1">
      <xs:simpleType name="C3" type="string" minOccurs="1"/>
   </complexType>
</complexType>
<xs:simpleType name="A2" type="string" minOccurs="1"/>

此外,我不能将现有的库用于 xml 模式,必须在代码中完成映射。

我已经尝试过这样的递归,但它不起作用:

function looping(arr, key) {
    if( typeof(arr[key]) == "object" && Object.keys(arr).length >= 1 ) {
        val = arr[key];
        for( k in value ) {
            looping(arr[key], k);
        }
    } else {
        var temp = arr[key];
    }
}

for(key in arr) {
    looping( arr, key);
}

标签: javascriptarraysobject

解决方案


假设这arr是一个对象数组,如下所示:

let arr = [
    {...},
    {...}
]

您需要编写一个函数,该函数将遍历数组中的每个对象并调用一个递归函数,该函数将检查对象是复杂对象还是简单对象。

如果它是复杂的对象,那么您应该再次调用该函数,递归地为对象调用,如果不是返回简单的对象。

您不需要编写一些额外的代码来返回 2 个级别或其他东西,递归过程会自然而然地到达那里。

我不确定你想要什么输出,要么是一个包含有效 xml 的字符串,要么是一个实际的 xml 对象,所以这是 jsfiddle,它从你的例子中获取一个对象数组,将其转换为字符串,然后使用 DOMParser 转换为 xml 对象(假设您想在浏览器中运行此代码):

https://jsfiddle.net/tara5/qk79Lcaz/

查看浏览器控制台以查看输出。

您可以修改函数以达到预期的效果。


推荐阅读