Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

looping through an object (tree) recursively

Is there a way (in jQuery or JavaScript) to loop through each object and it's children and grandchildren and so on?

If so... can I also read their name?

Example:

foo :{   bar:'',   child:{     grand:{       greatgrand: {         //and so on       }     }   } } 

so the loop should do something like this...

loop start    if(nameof == 'child'){      //do something    }    if(nameof == 'bar'){      //do something    }    if(nameof =='grand'){      //do something    } loop end 
like image 293
Val Avatar asked Mar 30 '10 23:03

Val


People also ask

Can you loop through an object JavaScript?

Object. key(). It returns the values of all properties in the object as an array. You can then loop through the values array by using any of the array looping methods.

What is a recursive object?

A recursive object type can repeat itself indefinitely or until some set limit is reached. The following object types are recursive within the IBM OpenPages® with Watson™ application: Business Entity (SOXBusEntity)


2 Answers

You're looking for the for...in loop:

for (var key in foo) {     if (key == "child")         // do something... }  

Be aware that for...in loops will iterate over any enumerable properties, including those that are added to the prototype of an object. To avoid acting on these properties, you can use the hasOwnProperty method to check to see if the property belongs only to that object:

for (var key in foo) {     if (!foo.hasOwnProperty(key))         continue;       // skip this property     if (key == "child")         // do something... } 

Performing the loop recursively can be as simple as writing a recursive function:

// This function handles arrays and objects function eachRecursive(obj) {     for (var k in obj)     {         if (typeof obj[k] == "object" && obj[k] !== null)             eachRecursive(obj[k]);         else             // do something...      } } 
like image 148
Andy E Avatar answered Oct 16 '22 15:10

Andy E


You can have a recursive function with a parse function built within it.

function parseObjectProperties (obj, parse) {   for (var k in obj) {     if (typeof obj[k] === 'object' && obj[k] !== null) {       parseObjectProperties(obj[k], parse)     } else if (obj.hasOwnProperty(k)) {       parse(k, obj[k])     }   } } 

I use the foo object of the OP, here how it works

var foo = {   bar:'a',   child:{     b: 'b',     grand:{       greatgrand: {         c:'c'       }     }   } }  // use this recursive function with a parse funciton function parseObjectProperties (obj, parse) {   for (var k in obj) {     if (typeof obj[k] === 'object' && obj[k] !== null) {       parseObjectProperties(obj[k], parse)     } else if (obj.hasOwnProperty(k)) {       parse(k, obj[k])     }   } } //***  // then apply to the property the task you want, in this case just console parseObjectProperties(foo, function(k, prop) {   console.log(k + ': ' + prop) })
like image 44
João Pimentel Ferreira Avatar answered Oct 16 '22 16:10

João Pimentel Ferreira