Javascript get Object property Name
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
}
});
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];
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