0

I have an object array with n dimensions by n is unknown. Here is the example of data :

Data = [{"value":{"Id":"1","Text":"abcd","Parent":""},"children":[{"value":{"Id":"2","Text":"abcd","Parent":"1"},"children":[{"value":{"Id":"3","Text":"abcd","Parent":"1"},"children":[{"value":...

I'd like to travel inside all this array to read the object data. How could I do that in javascript?

user2335149
  • 75
  • 2
  • 12
  • 2
    That would be [recursion](http://en.wikipedia.org/wiki/Recursion_(computer_science)). – Joseph May 15 '13 at 08:59
  • What do you mean by "*read the object data*"? Where exactly do you *want* to travel? – Bergi May 15 '13 at 09:02
  • I mean to get {"Id":"1","Text":"abcd","Parent":""} – user2335149 May 15 '13 at 09:02
  • @JosephtheDreamer What is wrong with recursion as long as it's finite (you speak from infinite regress)? – Christoph May 15 '13 at 09:02
  • 1
    @JosephtheDreamer: Or [iteration](http://en.wikipedia.org/wiki/Recursion_(computer_science)#Recursion_versus_iteration)… – Bergi May 15 '13 at 09:04
  • @user2335149: Now, that's just `Data[0].value` – Bergi May 15 '13 at 09:05
  • exact duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Bergi May 15 '13 at 09:06
  • @JosephtheDreamer - No, [this](http://stackoverflow.com/questions/16560796/how-to-travel-into-n-dimensional-object-array-in-javascript) would be recursion. I like the idea of travelling into a JS object - kind of like a browser-based Tron? – nnnnnn May 15 '13 at 09:46

2 Answers2

2

Use recursion, assuming all you want is value use something like this

var data = [
    {
        "value": {
            "Id": "1",
            "Text": "abcd",
            "Parent": ""
        },
        "children": [
            {
                "value": {
                    "Id": "2",
                    "Text": "abcd",
                    "Parent": "1"
                },
                "children": [
                    {
                        "value": {
                            "Id": "3",
                            "Text": "abcd",
                            "Parent": "1"
                        }
                    }
                ]
            }
        ]
    }
];

function travel(data){
    for(var i =0;i< data.length;++i){
        if(data[i].hasOwnProperty('value')){
            console.log('Value: ',data[i].value);
        }
        if(data[i].hasOwnProperty('children')){
            travel(data[i].children);
        }
    }
}

function collect(data) {
    var res= [];
    function travelAux(data){
        for(var i =0;i< data.length;++i){
            if(data[i].hasOwnProperty('value')){
                res.push(data[i].value);
            }
            if(data[i].hasOwnProperty('children')){
                travelAux(data[i].children);
            }
        }
    }
    travelAux(data);
    return res;
}
travel(data);
slobodan.blazeski
  • 1,040
  • 9
  • 20
1

You can do a recursive algorithm like this:

function travel(d){
        if (d[0] && d[0].value){
          console.log(d[0].value.Id);                          
        }
    if (d[0].children && typeof d[0].children == "object"){
            // if children exist call function recursively
            travel(d[0].children);
        }
}
travel(Data);

Have a look at the example which outputs the Id of your children to the console (F12). This is a simplfied version e.g. you have to consider the existence of multiple children in your array. But I think you get the idea.

Christoph
  • 50,121
  • 21
  • 99
  • 128