0

Possible Duplicate:
How do I enumerate the properties of a javascript object?

{ 
       347655082: {
                    album:{
                            title: "A",
                            genre: "Pop",
                          }
        },

        347655083: {

                    album:{
                            title: "B",
                            genre: "Rock", 
                          }
        }
}

Normally the "outside" key is the same, so I can easily target the nested objects. In the case the "outside" key is variable which can be anything.

albums = JSON.parse(json); //parse json storing it in albums

I cannot run a foreach on albums, say "albums has not method foreach".

albums.forEach(function(album, i){


}
Community
  • 1
  • 1
saeed
  • 3,861
  • 3
  • 25
  • 23
  • strictly, what you have there is a Javascript _object literal_, which looks somewhat like JSON, but has a looser syntax. This is what you get _after_ you've called `JSON.parse()` on real JSON. – Alnitak Aug 31 '12 at 18:21

3 Answers3

4

You can only use .forEach() on arrays. Your albums entity is an Object so you should use for ... in ...)

for (var key in albums) {
    if (albums.hasOwnProperty(key)) {
        // do something with albums[key]
        ...
    }
}

For code targetting node.js or any other ES5 implementation you could probably omit the if clause - it's only needed if you've got code that has unsafely added stuff to Object.prototype.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
1

ES5 has a great method for iteration object own enumerable properties - Object.keys(obj). I prefer the following iteration method:

Object.keys(albums).forEach(function (key) {
   console.log(albums[key]);
});
ActiveObject
  • 118
  • 5
-1

That's not valid json The valid json would be:-

    {
    "347655082": {
        "album": {
            "genre": "Pop", 
            "title": "A"
        }
    }, 
    "347655083": {
        "album": {
            "genre": "Rock", 
            "title": "B"
        }
    }
}

keys in json need to be double quoted as shown.

To validate json checkout jsonlint.com

James Kyburz
  • 13,775
  • 1
  • 32
  • 33
  • Yes, i forget the opening/closing brackets (corrected) but that does not answer my question. – saeed Aug 31 '12 at 18:19
  • @saeed: No, it's not only the closing bracket. Keys must be enclosed in quotes to be valid JSON. If what you posted is actually your `albums` object and you have no problems parsing the original JSON, then say so. Then there is no reason to mention JSON at all, it's just confusing. – Felix Kling Aug 31 '12 at 18:20
  • @FelixKling it's a common mistake - the OP has merely confused an object literal with JSON, and then picked the wrong question title... – Alnitak Aug 31 '12 at 18:24
  • 1
    @Alnitak: I know and you are probably right, but if we don't point this out explicitly, it will never get better ;) – Felix Kling Aug 31 '12 at 18:26
  • I stand corrected. I should not have used term json. Its a javascript object literal. – saeed Aug 31 '12 at 18:28