4

Given an object like this:

{ name: "joe" }

I want to get the value "name". I know I can use the for construct to iterate over the properties in an object, but the objects I'll be dealing with will always have a single key:value pair, and I won't know the name of the property. To further illustrate:

var a = { age: 24 };
console.log(myFunc(a)) // Displays "age"

var b = { job: "cook" };
console.log(myFunc(b)) // Displays "job"

Is there anyway to do this without iterating over the object? Also I'd like to do this in pure Javascript. No frameworks/libs involved.

mellowsoon
  • 22,273
  • 19
  • 57
  • 75
  • Maybe you should use an array instead `a=["age",24"]`, and then you get the name with `a[0]` – some Oct 20 '10 at 22:08

5 Answers5

7

It is good practice to use .hasOwnProperty to ensure you aren't returning a property from the Object prototype:

function myFunc(obj) {
    for (var prop in obj) {
        if (obj.hasOwnProperty(prop)) return prop;
    }
}
Ryan Tenney
  • 1,812
  • 3
  • 16
  • 29
  • I'm curious if it's worth the additional function call. My testing (In Firefox) shows the object prototypes always come last during iteration. Maybe that's not the case in all browsers? – mellowsoon Oct 20 '10 at 22:22
  • 1
    @mellowsoon, I was about to come with that, in IE8 and below, inherited properties are enumerated at first! (try [this test](http://jsfiddle.net/cmsalvado/cuFeK/) in IE<=8), if you want the first enumerable **own property** you should use `hasOwnProperty`, although I would use `if (Object.prototype.hasOwnProperty.call(obj, prop))` (maybe storing a ref. to the method to make it fast), to make it *safer*, since an object *could* have a property named `hasOwnProperty`, and that property will *shadow* the method from `Object.prototype`. Anyway this should be the accepted answer, +1 to @Ryan :) – Christian C. Salvadó Oct 21 '10 at 02:25
  • 1
    After researching the hasOwnProperty() function a bit more, I see how important it is. So I've changed this to the correct answer. – mellowsoon Oct 23 '10 at 04:33
3

This seems to be about the best you can get:

function myFunc(v) {
  for (var x in v) { return { prop: x, val: v[x] }; }
  return null;
};
John Fisher
  • 22,355
  • 2
  • 39
  • 64
2

Nope, iteration is the only well-supported way to get the property name. So for...in time it is. Just hide it in a function and it'll look fine.

However, it might also be worth thinkin about whether you should be using a different kind of object for your purpose, say, {"property": "age", "value": 24}, or even ["age", 24]

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
2

You can iterate over the object's properties and simply return the first one.

function myFunc(obj) {
    for (var prop in obj) {
        return prop;
    }
}

Edit: oops, you wanted the property name, not the value

Phil
  • 157,677
  • 23
  • 242
  • 245
1

Why not iterate? It's just one step. I don't think you can get it in any other way. You can even break after the first step if it makes you feel better.

Alin Purcaru
  • 43,655
  • 12
  • 77
  • 90