Javascript get Object property Name

javascript get object key name
javascript get object properties
javascript object get value by key
javascript get object name
javascript object keys
javascript get property name as string
get first property name of object javascript
javascript map

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.

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];

Object.getOwnPropertyNames(), An object property name can be any valid JavaScript string, you need to do is to prefix a getter method with get and a setter method with set . JavaScript provides a bunch of good ways to access object properties. The dot property accessor syntax object.property works nicely when you know the variable ahead of time. When the property name is dynamic or is not a valid identifier, a better alternative is square brackets property accessor: object [propertyName].

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
    }
});

Working with objects, Originally published in the A Drip of JavaScript newsletter. But with ECMAScript 5 we get access to Object.keys which eliminates this tedious boilerplate. getOwnPropertyNames which is less strict about which property names it will retrieve� Getting value out of maps that are treated like collections is always something I have to remind myself how to do properly. In this post I look at JavaScript object iteration and picking out values from a JavaScript object by property name or index.

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

Retrieving Property Names with `Object.getOwnPropertyNames , Properties are the values associated with a JavaScript object. A JavaScript object is a x = "age"; person[x]. The expression must evaluate to a property name. Accessing JavaScript Properties. The syntax for accessing the property of an object is: objectName.property // person.age. or. objectName [ "property" ] // person ["age"] or. objectName [ expression ] // x = "age"; person [x] The expression must evaluate to a property name.

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
    }

JavaScript Object Properties, const { name } = hero is an object destructuring. The destructuring defines a variable name with the value of property name . When you get used� you can access property x of c as follows: var cx = foo['c'] ['x'] If a property is undefined, an attempt to reference it will return undefined (not null or false ): foo['c'] ['q'] === null // returns false foo['c'] ['q'] === false // returns false foo['c'] ['q'] === undefined // returns true. share. Share a link to this answer.

3 Ways To Access Object Properties in JavaScript, Property values are accessible using the dot notation: // get property values of the object: alert ( user . name ) ; // John� a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}). Therefore an object literal is not an array, and you can only access the properties using their explicit name or a for loop using the in keyword.

Objects, An object. A property name of the object. A property descriptor object that has four But the accessor properties have the [[Get]] and [[Set]] attributes instead of � So, the name is stored in _name property, and the access is done via getter and setter. Technically, external code is able to access the name directly by using user._name. But there is a widely known convention that properties starting with an underscore "_" are internal and should not be touched from outside the object.

Understanding JavaScript Object Property Types and Their Attirbutes, Therefore, if you've an object's property name stored in a JavaScript variable, you can get its value using the square bracket notation, as shown in the following� The Object.getPrototypeOf () method returns the prototype (i.e. the value of the internal [ [Prototype]] property) of the specified object. JavaScript Demo: Object.getPrototypeOf ()

Comments
  • 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.
  • second one should probably be typeB.
  • This returns ["typeA"] not "typeA"