75

I passed the following object:

var myVar = { typeA: { option1: "one", option2: "two" } }

I want to be able to pull out the key typeA from the above structure.

This value can change each time so next time it could be typeB.

So I would like to know if there is a way for me to do something like the following pseudo code:

var theTypeIs = myVar.key();

This way when I can pass this object and I can pull out the first value of the object, in this case it is typeA and then based on that I can do different things with option1 and option2.

4 Answers 4

133

If you know for sure that there's always going to be exactly one key in the object, then you can use Object.keys:

theTypeIs = Object.keys(myVar)[0];
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it will always be one object, the object is a param to a function being called and I just needed to tell what the 'key' was so I can determine what action to take with the 'vaule' portion.
18

Like the other answers you can do theTypeIs = Object.keys(myVar)[0]; to get the first key. If you are expecting more keys, you can use

Object.keys(myVar).forEach(function(k) {
    if(k === "typeA") {
        // do stuff
    }
    else if (k === "typeB") {
        // do more stuff
    }
    else {
        // do something
    }
});

1 Comment

second one should probably be typeB.
15

If you want to get the key name of myVar object then you can use Object.keys() for this purpose.

var result = Object.keys(myVar); 

alert(result[0]) // result[0] alerts typeA

1 Comment

This returns ["typeA"] not "typeA"
0

I was searching to get a result for this either and I ended up with;

const MyObject = {
    SubObject: {
        'eu': [0, "asd", true, undefined],
        'us': [0, "asd", false, null],
        'aus': [0, "asd", false, 0]
    }
};

For those who wanted the result as a string:

Object.keys(MyObject.SubObject).toString()

output: "eu,us,aus"

For those who wanted the result as an array:

Array.from(Object.keys(MyObject))

output: Array ["eu", "us", "aus"]

For those who are looking for a "contains" type method: as numeric result:

console.log(Object.keys(MyObject.SubObject).indexOf("k"));

output: -1

console.log(Object.keys(MyObject.SubObject).indexOf("eu"));

output: 0

console.log(Object.keys(MyObject.SubObject).indexOf("us"));

output: 3

as boolean result:

console.log(Object.keys(MyObject.SubObject).includes("eu"));

output: true


In your case;

var myVar = { typeA: { option1: "one", option2: "two" } }

    // Example 1
    console.log(Object.keys(myVar.typeA).toString()); // Result: "option1, option2"

    // Example 2
    console.log(Array.from(Object.keys(myVar.typeA))); // Result: Array ["option1", "option2" ]
    
    // Example 3 as numeric
    console.log((Object.keys(myVar.typeA).indexOf("option1")>=0)?'Exist!':'Does not exist!'); // Result: Exist!
    
    // Example 3 as boolean
    console.log(Object.keys(myVar.typeA).includes("option2")); // Result: True!
    
    // if you would like to know about SubObjects
    for(var key in myVar){
      // do smt with SubObject
      console.log(key); // Result: typeA
    }

    // if you already know your "SubObject"
    for(var key in myVar.typeA){
      // do smt with option1, option2
      console.log(key); // Result: option1 // Result: option2
    }

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.