简体   繁体   English

在JavaScript中显示多维对象数组的结果

[英]Displaying results from array of multidimensional objects in javascript

I don't have much experience in Javascript, so I ask for help from you. 我没有Java方面的丰富经验,所以我需要您的帮助。 I have array of multidimensional objects represented like this: 我有这样表示的多维对象数组:

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""
        },
    },
}

How can I recursively loop through this array and display results while keeping track of the parent object in case I have to return back 2 levels, such as in this case with "Ob7"? 在必须返回2个级别(例如在这种情况下使用“ Ob7”)的情况下,如何在该数组中递归循环并显示结果,同时跟踪父对象?

The results have to be displayed in xml schema way(If an object has children is complexType, if not, simpleType: 结果必须以xml模式显示(如果一个对象有子对象为complexType,否则为simpleType:

<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"/>

Also, I can't use already existing libraries for xml schema, the mapping has to be done in the code. 另外,我不能将现有的库用于xml模式,必须在代码中完成映射。

I already tried something like this for the recursion, but It's not working: 我已经为递归尝试了类似的方法,但是没有用:

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);
}

Assuming that arr is a array of objects, which looks like this: 假设arr是一个对象数组,如下所示:

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

you need to write a function which will iterate through each object in the array and call a recursive function which will check if object is a complex object or a simple object. 您需要编写一个函数,该函数将遍历数组中的每个对象并调用一个递归函数,该函数将检查对象是复杂对象还是简单对象。

If it's complex object than you should call that function again, recursively for the object, if not return simple object. 如果是复杂对象,则应递归地为该对象再次调用该函数,如果没有返回简单对象,则应递归调用该函数。

You don't need to write some additional code to get back 2 levels or something, recursive process will get there naturally. 您无需编写其他代码即可获得2个级别或其他内容,递归过程将自然而然地到达那里。

I'm not sure what the output you want, either a string which contains valid xml or an actual xml object, so here's the jsfiddle which takes an array of objects from you example, converts it to string and then to xml object using DOMParser (assuming you want to run this code in a browser): 我不确定您想要的输出是包含有效xml的字符串还是实际的xml对象,因此这是jsfiddle,它从您的示例中获取对象数组,将其转换为字符串,然后使用DOMParser(假设您想在浏览器中运行此代码):

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

Check out the browser console to see the output. 签出浏览器控制台以查看输出。

You can modify functions to achieve a desired effect. 您可以修改功能以获得所需的效果。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM