2

I wonder if someone can point me into the right direction on this. When I have an object, I usually read its properties via FOR IN LOOP. And since I know what the properties are because I created the very object, I know what keys and their values are. I would like to know if there are any other ways such as recursive approach to read through an object and its properties.

So here is the object for an example:

var mObj = {};
mObj.mArr = [];
mObj.mArr.push({id:['id1','id2','id3']});
mArr.push({days:['Monday','Tuesday','Wednesday','Thursday']});
mObj.mArr.push({colors:['orange','red','blue','green','yellow','white']});

As you can see, I have declared the mObj and within it, I have declared mArr as an Array. And then I am pushing three objects within mArr:

  1. id
  2. days
  3. colors

if I wanted to find out mArr length, I would write something like:

alert(mObj.mArr.length);

And if I wanted to loop through the object, I would have something like this:

for(var a in mObj.mArr[0])
{
   alert(mObj.mArr[0][a]);
}

I wonder if I can just right one function and pass the parent object which happens to be "mObj" here, and this recursive function would through the "mObj" and list all of its properties from head to toe.

I would appreciate any input on this, please.

Thanks a lot.

5
  • 1
    Yes, that is possible. What exactly are you having problems with? Commented Aug 18, 2011 at 3:57
  • Why wouldn't you just write out the full object in literal notation? Commented Aug 18, 2011 at 3:58
  • 2
    When you say 'list all of its properties' do you just mean string output with no practical use? If you do then just serialize the object to JSON. One function, recursive, already written, and pretty output (although if any of your object's properties are functions it could present issues). Commented Aug 18, 2011 at 3:58
  • Felix, there are time, I am catching objects from the server side which aren't written by me, I just want to print out all the details right upfront...more like having a utility function which just spits out what has just been processed by it. Commented Aug 18, 2011 at 7:18
  • Matt, I have been coding this way for a very long time so It has now become more like a style. Commented Aug 18, 2011 at 7:21

1 Answer 1

17

Here's a basic example:

var mObj = {};
mObj.mArr = [];
mObj.mArr.push({id:['id1','id2','id3']});
mObj.mArr.push({days:['Monday','Tuesday','Wednesday','Thursday']});
mObj.mArr.push({colors:['orange','red','blue','green','yellow','white']});

function r(obj) {
    if (obj)
        for (var key in obj) {
            if (typeof obj[key] == "object")
                r(obj[key]);
            else if (typeof obj[key] != "function")
                console.log(obj[key])
        }

    return;
} 

r(mObj);

if there is an object not a function call the function again, else console log the value if it is not a function.

Sign up to request clarification or add additional context in comments.

3 Comments

Joey...You hit the nail right on. I cannot thank you enough. I will plug it in tomorrow and see what I come up with but so far it looks to be what I was searching for. thanks a million
No problem, be sure to accept some answers though as explained here: meta.stackoverflow.com/questions/16721/
Joe - Thanks a ton. I don't have a lot of experience with recursive and it has a tendency to melt my brain but this definitely was the perfect blank slate to figure out my problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.