2

In javascript, is there a way to treat a JSON keyname as a variable?

For instance, below I want to perform (any set of actions) on the value of a different key in JSON each time, by telling it which json key to get by feeding the keyname to the function as a parameter.

The basic idea here is: Make a function to do (x) no matter what JSON it's given. I just need to tell it what key I want.

var myObj = {name: "John", age: 31}; // dataset #1
var myObj2 = {month: "January", day: 20}; //dataset #2

function myFunction(jsonName, variableKeyName) {   

  var variableKeyValue = jsonName + "." + variableKeyName;  //wrong way to do this, apparently.
 
  console.log(variableKeyValue); 
}

myFunction("myObj", "name"); //want this to log "John", not the string "myObj.name".
myFunction("myObj2", "day"); //want this to log "20", not the string "myObj2.day". 

Is this possible? How do get the function to assign the VALUE of the string I'm building with the parameter, rather than just the string as "jsonNameX.variableKeyNameX"?

2 Answers 2

2
const myObj = {name: "John", age: 31}; // dataset #1
const myObj2 = {month: "January", day: 20}; //dataset #2

function myFunction(json, variableKeyName) {   
  const variableKeyValue = json[variableKeyName]; 
  console.log(variableKeyValue); 
}

myFunction(myObj, "name"); // this log "John"
myFunction(myObj2, "day"); // this log 20 

or you can access to global context using "this" keyword and variable name (instead variable identifier == reference)

const myObj = {name: "John", age: 31}; // dataset #1
const myObj2 = {month: "January", day: 20}; //dataset #2

function myFunction(jsonName, variableKeyName) {   
  const variableKeyValue = this[jsonName][variableKeyName]; 
  console.log(variableKeyValue); 
}

myFunction("myObj", "name"); // this log "John"
myFunction("myObj2", "day"); // this log 20 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Marco Mazzone! this is exactly what I was looking for.
1

Yes you can do it. You can get the JSON value like this:

jsonName.variableKeyName;

but you can also get it like this:

jsonName["variableKeyName"];

(https://www.w3schools.com/js/js_json_syntax.asp)

So you can change your code in this way:

var myObj = {name: "John", age: 31}; // dataset #1
var myObj2 = {month: "January", day: 20}; //dataset #2

function myFunction(jsonName, variableKeyName) {  
  var variableKeyValue = jsonName[variableKeyName];
  console.log(variableKeyValue ); 
}

myFunction(myObj, "name"); //this prints "John"
myFunction(myObj2, "day"); //this prints "20" 

2 Comments

Please tell me if you want something different
OMG thank you, @Nick Pantelidis! I don't know why my brain just didn't make that leap but this this is exactly what I needed. I don't even know why I was trying to concatenate the variables together as a string in the first place. Thanks!

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.