iegorov
4/22/2014 - 4:18 AM

## Each recursive iteration.

Each recursive iteration.

``````{
"branches": [
{
"@xsi.type": "class",
"id": "_77607",
"name": "нет",
"predicate": {
"literal": "2=2",
"dialect": "V1",
"root": {
"token": "equal",
"begin": 0,
"end": 3,
"branches": [
{
"token": "const_int",
"begin": 0,
"end": 1
},
{
"token": "const_int",
"begin": 2,
"end": 3
}
]
}
},
"order": "0"
},
{
"@xsi.type": "class",
"id": "_77601",
"name": "да",
"predicate": {
"literal": "1=1",
"dialect": "V1",
"root": {
"token": "equal",
"begin": 0,
"end": 3,
"branches": [
{
"token": "const_int",
"begin": 0,
"end": 1
},
{
"token": "const_int",
"begin": 2,
"end": 3
}
]
}
},
"order": "1"
},
{
"@xsi.type": "choose",
"id": "",
"branches": [
{
"@xsi.type": "class",
"id": "",
"name": "да",
"predicate": {
"literal": "3.1 = 3.1",
"dialect": "V1",
"root": {
"token": "equal",
"begin": 0,
"end": 9,
"branches": [
{
"token": "const_float",
"begin": 0,
"end": 3
},
{
"token": "const_float",
"begin": 6,
"end": 9
}
]
}
},
"order": "3"
},
{
"@xsi.type": "class",
"id": "",
"name": "нет",
"predicate": {
"literal": "3.2 = 3.2",
"dialect": "V1",
"root": {
"token": "equal",
"begin": 0,
"end": 9,
"branches": [
{
"token": "const_float",
"begin": 0,
"end": 3
},
{
"token": "const_float",
"begin": 6,
"end": 9
}
]
}
},
"order": "4"
},
{
"@xsi.type": "class",
"id": "",
"name": "подумаю",
"order": "5"
}
],
"predicate": {
"literal": "3=3",
"dialect": "V1",
"root": {
"token": "equal",
"begin": 0,
"end": 3,
"branches": [
{
"token": "const_int",
"begin": 0,
"end": 1
},
{
"token": "const_int",
"begin": 2,
"end": 3
}
]
}
},
"order": "2"
},
{
"@xsi.type": "class",
"id": "_77602",
"name": "подумаю",
"order": "6"
}
]
}``````
``````	// Add order value each branches
var iterator = 0;

_.each(data.branches, function(branch) { recursiveFunction(branch) });

function recursiveFunction(branch) {
branch.order =  (iterator++).toString();
if (branch.branches) {
_.each(branch.branches, function(branch) {
recursiveFunction(branch)
});
}

}
console.log(data);``````
``````
var data = {};
data.branches = [
{
"@xsi.type": "class",
"id": "_77607",
"name": "нет",
"predicate": {
"literal": "2=2",
"dialect": "V1",
"root": {
"token": "equal",
"begin": 0,
"end": 3,
"branches": [
{
"token": "const_int",
"begin": 0,
"end": 1
},
{
"token": "const_int",
"begin": 2,
"end": 3
}
]
}
}
},
{
"@xsi.type": "class",
"id": "_77601",
"name": "да",
"predicate": {
"literal": "1=1",
"dialect": "V1",
"root": {
"token": "equal",
"begin": 0,
"end": 3,
"branches": [
{
"token": "const_int",
"begin": 0,
"end": 1
},
{
"token": "const_int",
"begin": 2,
"end": 3
}
]
}
}
},
{
"@xsi.type": "choose",
"id": "",
"branches": [
{
"@xsi.type": "class",
"id": "",
"name": "да",
"predicate": {
"literal": "3.1 = 3.1",
"dialect": "V1",
"root": {
"token": "equal",
"begin": 0,
"end": 9,
"branches": [
{
"token": "const_float",
"begin": 0,
"end": 3
},
{
"token": "const_float",
"begin": 6,
"end": 9
}
]
}
}
},
{
"@xsi.type": "class",
"id": "",
"name": "нет",
"predicate": {
"literal": "3.2 = 3.2",
"dialect": "V1",
"root": {
"token": "equal",
"begin": 0,
"end": 9,
"branches": [
{
"token": "const_float",
"begin": 0,
"end": 3
},
{
"token": "const_float",
"begin": 6,
"end": 9
}
]
}
}
},
{
"@xsi.type": "class",
"id": "",
"name": "подумаю"
}
],
"predicate": {
"literal": "3=3",
"dialect": "V1",
"root": {
"token": "equal",
"begin": 0,
"end": 3,
"branches": [
{
"token": "const_int",
"begin": 0,
"end": 1
},
{
"token": "const_int",
"begin": 2,
"end": 3
}
]
}
}
},
{
"@xsi.type": "class",
"id": "_77602",
"name": "подумаю"
}
];``````