3

Given I have a javascript object, is there a way to iterate over all the primitive subproperties?

For instance, if I have an object

{
  foo: 17,
  bar: {
    a: 2,
    b: 7
  }
}

I would like to iterate over foo, bar.a, and bar.b.

Please keep in mind I prefer to iterate over Object.keys() rather than using a for/in loop, although I'm sure I could translate any for/in loop responses into an Object.keys() iteration.

ferson2020
  • 3,015
  • 3
  • 18
  • 26

2 Answers2

7

You can use a recursive function like this:

var x = {
    foo: 17,
    bar: {
        a: 2,
        b: 7
    }
}

function parseObject(something) {
    var keys = Object.keys(something);
    for (var i = 0; i < keys.length; i++) {
        if (typeof something[keys[i]] === 'object') parseObject(something[keys[i]])
        else console.log(keys[i] + " : " + something[keys[i]]);
    }
}
parseObject(x);

Which generates the output:

foo : 17 
a : 2 
b : 7 

A note on this function. It recurses over anything that is an object. For instance, if you had an array in the object, you would get separate lines for each item in the array.

So for the following object:

var x = {
    foo: 17,
    bar: {
        a: 2,
        b: 7
    },
    foobar: [1,2,3]    
}

The output would appear:

foo : 17 
a : 2 
b : 7 
0 : 1 
1 : 2 
2 : 3 

There are obviously ways to handle this, but you will need to tailor the function to meet your requirements.

John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • OK thank you, I can see how that would definitely work for what I need. I wanted to check if there is a built-in function or more standard way to do this, but barring that, this is what I would use. – ferson2020 Jan 25 '13 at 16:36
  • It fails when `something[keys[i]]` is `null` because `typeof null === 'object'` is true. Then error thrown is "Uncaught TypeError: Cannot convert undefined or null to object" I would add `if(something[keys[i]])` to the original condition to ensure it's truthy. – Misael Abanto Feb 10 '22 at 11:17
0

Update: This is how you can do it (taken from one of the links below):

for (var key in validation_messages) {
   var obj = validation_messages[key];
   for (var prop in obj) {
      alert(prop + " = " + obj[prop]);
   }
}

This is a possible duplicate of :

Iterate through object properties

How to Loop through plain JavaScript object with objects as members?

you can find your answer in the above posts.

Community
  • 1
  • 1
Mehdi Karamosly
  • 5,388
  • 2
  • 32
  • 50
  • 4
    Linking to other SO questions without adding value is a comment, not an answer. – John Koerner Jan 25 '13 at 16:26
  • 1
    These questions are asking about iterating through properties; I'm looking to iterate through properties and subproperties recursively. – ferson2020 Jan 25 '13 at 16:30
  • I also want to cover an arbitrary depth, so I want the primitive properties, plus the primitive subproperties of the non-primitive properties, plus the primitive subproperties of the non-primitive subproperties of the non-primitive properties, etc. – ferson2020 Jan 25 '13 at 16:33
  • you can add a check and make it a recursive call based on checking of the element is an object using `typeof element === 'object';`. – Mehdi Karamosly Jan 25 '13 at 16:34