Javascript: Retrieve Object Property Names

Posted by Jason on Stack Overflow See other posts from Stack Overflow or by Jason
Published on 2010-04-26T23:47:05Z Indexed on 2010/04/26 23:53 UTC
Read the original article Hit count: 118

Filed under:

I'm trying to write a function that needs to know the property names of an object being passed in, like so:

var data = { "key1":"value1", "key2":"value2", etc}
               ^ i want the string value "key1"

How do I retrieve the string "key1" from data? I know I can set a property dynamically like data[prop]=value but i want to know what prop is from an object passed in.

If that doesn't make sense I suppose I could try to explain more. Thanks!

I eventually want to do something like:

for (var i = 0; i<data.length; i++)
{
    var name = data[i].getPropertyName() <--- not a real function
    // do stuff
}

© Stack Overflow or respective owner

Related posts about JavaScript