1

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.

6
  • Since you are already passing the object and its key, you already know the key, right? so the key is the string you are looking for Commented Jan 10, 2018 at 11:17
  • right, but if i say keyname=obj.input1; then i have stored the sub-object in keyname, not the name of the key. Or am i completely off the track here? Commented Jan 10, 2018 at 11:27
  • 1
    What you are saying is correct. I am just wondering why you can't just do keyname=input1? Commented Jan 10, 2018 at 11:28
  • do you hand over a string or a reference/value? Commented Jan 10, 2018 at 11:53
  • in the real-world i'm planning to do this: CurrItem=obj.input1; // stores the sub-object in CurrItem to work with later) CurrItemName=getTheNameOfThisAsString(CurrItem); // get the name of the sub-object to output it. If any way possible, i would like to write down the specific sub object only once, and the script does the rest itself (getting its name as string, and work with the sub objects). If not, of course i could just additionally write CurrItemName="input1". Commented Jan 10, 2018 at 12:15

3 Answers 3

1

Use Object.keys() to access keys of your object.

var obj = { "input1": { "type": "input", "value": "aaa" }, "input2": { "type": "checkbox", "value": "bbb" } };
var keys = Object.keys(obj);
console.log(keys);

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

2 Comments

This outputs an Array with "input1" and "input2" as values. What I'd need is the string equivalent of obj.input1 ... returning "input1"
Why not split on . and get the last value?
0

I am not sure what you are trying to accomplish, but I assume that you do not know the key, and thus you are sending object.key to your method.

How about changing the method to take the object and your object.key as parameters?

You call your method like this: getTheNameOfThisAsString(obj, obj.input1);

function getTheNameOfThisAsString(obj, subObject){
 var keys = Object.keys(obj);
 for(var key in keys){
  if(subObject.value == obj[key].value){
   return key;
  }
 }
}

Comments

0

From your comments, I was wondering if you can store your sub-object like this: currItem=obj["input1"], if that is the case, you already have the key as a string, and there is no need for an explicit method.

Here is an example:

var currItemName = "input1";
var currItem=obj[currItemName];

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.