У меня нет большого опыта работы с 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-схемы (если у объекта есть дочерние элементы, это 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"/>
Кроме того, я не могу использовать уже существующие библиотеки для 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);
}