Quello che vuoi fare non credo sia possibile se non modificando l'oggetto(json) originario. Questo perche per poter scorrere l'intero oggetto ogni nodo dovrebbe avere un riferimento al nodo padre cosi da poter scorrerlo sia in discesa che in salita. Se il json lo generi tu e quindi lo puoi modificare farei cosi:
per prima cosa aggiungi 2 funzioni per ogni nodo(parent e next), es:
codice:
var myjson = { "name": "root", "id": "N0", "type": "root", parent: function(){return null;}, next: function(){return null;},
"children": [
{
"name": "livello1",
"size": 2000,
"id": "N1",
parent: function(){return myjson;},
next: function(){return myjson.children[1];},
"children": [
{
"name": "livello2",
"size": 1000,
"id": "N11",
parent: function(){return myjson.children[0];},
next: function(){return myjson.children[0].children[1];},
"children": null
},
{
"name": "livello2",
"size": 1000,
"id": "N12",
parent: function(){return myjson.children[0];},
next: function(){return null;},
"children": null
}
]
},
{
"name": "livello1",
"size": 2000,
"id": "N2",
parent: function(){return myjson;},
next: function(){return myjson.children[2];},
"children": [
{
"name": "livello2",
"size": 1000,
"id": "N21",
parent: function(){return myjson.children[1];},
next:function(){return myjson.children[1].children[1];},
"children": null
},
{
"name": "livello2",
"size": 1000,
"id": "N22",
parent: function(){return myjson.children[1];},
next: function(){return null;},
"children": null
}
]
}
]
};
A questo punto si può usare una funzione ricorsiva, qualcosa del genere:
codice:
isUp = false;
function SeekAndPush(json,nodeid){
if(isUp){
if(json.next() != null){
isUp = false;
SeekAndPush(json.next(), nodeid);
}else if(json.parent() != null){
SeekAndPush(json.parent(), nodeid);
}
}else{
if(json.id == nodeid) {
activenodes.push(json);
}
if(json.children != null){
SeekAndPush(json.children[0], nodeid);
}else if(json.next() != null){
SeekAndPush(json.next(), nodeid);
}else if(json.parent() != null){
isUp = true;
SeekAndPush(json.parent(), nodeid);
}
}
}