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