I'd like to get a key name of a JSON/JavaScript object as string. Maybe this is totally easy, but i just can't figure it out.
I have this object (simplified example, there are reasons to name the keys as strings):
var obj = {
"input1": {
"type": "input",
"value": "aaa"
},
"input2": {
"type": "checkbox",
"value": "bbb"
}
}
And now i would like to do something like this:
currentInputName = getTheNameOfThisAsString(obj.input1);
console.log(currentInputName); // output should be "input1"
currentInputName = getTheNameOfThisAsString(obj.input2);
console.log(currentInputName); // now output should be "input2"
I'm trying this with Object.keys() and Object.getOwnPropertyNames(), but both return type and value to me, so they are outputting the keys of the object specified, not the object name itself.