简体   繁体   中英

Displaying results from array of multidimensional objects in javascript

I don't have much experience in Javascript, so I ask for help from you. 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"?

The results have to be displayed in xml schema way(If an object has children is complexType, if not, 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.

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:

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.

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):

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

Check out the browser console to see the output.

You can modify functions to achieve a desired effect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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