27

Is there a way to get the name of the first property of a JSON object?

I'd like to do something like this:

var firstProp = jsonObj[0];

edit: I'm getting a JSON object which hold categories of arrays with image URLs.

like so:

{
  "category1":["image/url/1.jpg","image/url/2.jpg"],
  "category2":["image/url/3.jpg","image/url/4.jpg"]
}

I am then iterating through the object to insert the images, and all I really wanted was an elegant way to see which category was inserted first. At first I just did

for (var cat in images) {
    if (i==0) firstCat = cat;
    ...
}

But that some how "felt" ugly... So it was basically just a question of elegance.

2

5 Answers 5

48
console.log(jsonObj[Object.keys(jsonObj)[0]]);
Sign up to request clarification or add additional context in comments.

4 Comments

It would be better to write a small explanation of why this is the answer, and why yours is better than the others.
As I understand it, Object.keys(jsonObj) will create an array of the keys of the object which can be accessed using [] notation. [0] is the first item in the array. Adding this as the key lookup for the jsonObject (inside its own square brackets) is essentially saying this; "make an array of all the keys in the object, give me the first of these, now look up the values for that key, in the original object".
I actually like this one better
Meaning: Give me the attribute named by the key index 0 from jsonObject. Or ... Object.keys(jsonObj)[0] = Take the name of the key index zero, now give me the content of jsonObj[ that key ]
35

The order of the properties of an object are not guaranteed to be the same as the way you put them in. In practice, however, all major browsers do return them in order. So if you're okay with relying on this...

var firstProp;
for(var key in jsonObj) {
    if(jsonObj.hasOwnProperty(key)) {
        firstProp = jsonObj[key];
        break;
    }
}

Also note that there's a bug in Chrome regarding the ordering, in some edge cases it doesn't order it in the way they were provided. As far as it changing in the future, the chances are actually pretty small as I believe this is becoming part of the standard so if anything support for this will only become official.

All things considered, though, if you really, really, absolutely, positively, want to be sure it's going to be in the right order you need to use an array. Otherwise, the above is fine.

Related question: Elements order - for (… in …) loop in javascript

3 Comments

But what if that changes in the future?
Just a note to say that object property enumeration order didn't make it into the ECMAScript 5 spec, Chrome has stood fast in its behaviour and now other browsers are following suit, so @the_drow's fears were well-founded.
Please note that arrays are a common exception to this rule, as will always iterate on the numerical order. The best compliant solution is to just use Map or Set, which have a guaranteed order regardless of circumstances.
14

There isn't a "first" property. The properties of an object are unordered.

You can get whatever one the JS engine decides to provide first with a loop.

function maybe_first_in_object(ob) {
    for (var props in ob) {
        return prop;
    }
}

… but if the order matters, use an array not an object.

1 Comment

Good point that they're not guaranteed to be the order you specified them in -- upvoted. I would make sure to check hasOwnProperty() though.
2

Great question. I don't know of any way besides iterating in a for-in loop. You only need to iterate once though. For safety, ensure it's a known property [more info].

for (var propName in jsonObj) {
    if (jsonObj.hasOwnProperty(propName)) {
        return propName;    // or do something with it and break
    }
}

Edited to be extra clear that you're iterating over the property names, not their values.

Comments

1

When using jQuery, you can also use $.each to iterate over a JSON object. Looks cleaner IMO than using a for-loop

var items = {
    'item1': 'content',
    'item2': 'content',
    'item3': 'content'
}

$.each(items, function() { 
    console.log($(this)[0])
})

Comments

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.