1

I am trying to get the first object within an object.

I have something like

var object = {
     task:{
       config:'auto',
       title :'test'
     },
     prop:'switch',
     time:'5min'
}

My problem is task object could be named differently. so it could be

var object = {
     task2:{
       config:'manual',
       title :'test2'
     },
     prop:'switch',
     time:'5min'
}

I can't use object.task1.config because task name could changed. The task object will always be the first object though.

I want to get the task object no matter what name it has. How do I accomplish that? Thanks!

4
  • Could it have other properties than prop, time or the one you want ? Commented Nov 13, 2013 at 20:42
  • no it won't have other properties. I just need to get the first task object. Commented Nov 13, 2013 at 20:43
  • @FlyingCat are there other properties in the object that are also objects, or is task the only one? Commented Nov 13, 2013 at 20:47
  • 1
    Objects properties don't really have a concept of "first" - if you need properties to be in a specific order you have to use an array. Commented Nov 13, 2013 at 20:48

2 Answers 2

2

To get the first property value of an object in modern browsers you could use the Object.keys method:

var myObject = {
    whoTheHellKnows: 'foo',
    notUsed: 'whatever'
};

var firstProperty = myObject[Object.keys(myObject)[0]];

Working example

Edit: if you do not trust the ordering of the properties (which you should not) AND the task object is the only nested object in your objects, you can rely on type interrogation to find it:

var myObject = {
    task123215452: { dahKey: 'foo' },
    notUsed: 'whatever',
    somethingElse: 42
};

var taskObject;

for (var key in myObject) { 
    if (typeof myObject[key] === 'object' && !(myObject[key] instanceof Array)) {
        taskObject = myObject[key];
        break;
    }
}

Working example

Sign up to request clarification or add additional context in comments.

5 Comments

Is Object.Keys() guaranteed to return the keys in the same order they were defined ?
No, there's no guarantee regarding the order.
From the docs: "The ordering of the properties is the same as that given by looping over the properties of the object manually."
@jbabey ...which is implementation-dependent
@bfavaretto my edit should be order-independent, though it relies on there only being a single nested object literal.
2

If you need to access the first key and you don't know the key name, you should really be using an array instead of an object. Objects don't have the concept of a "first" key.

If that's not an option, you're left with for..in or Object.keys, which may fail on some JavaScript implementations because the language specification does not enforce the order of object key enumerations. However, in practice, it will work on current browsers, as they all iterate the keys in the order they were declaredcitation needed thanks to jbabey.

2 Comments

Your citation: "all major browsers support an iteration order based on the earliest added property coming first"
Interesting IE edge case mentioned there!

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.