How to get object values with dynamic variable from json [duplicate]

15
October 09, 2019, at 5:00 PM

This question already has an answer here:

  • Dynamically access object property using variable 13 answers

I want to get the value of an object field from JSON. I have a dynamic variable let check_permission_key = 'ratingscalename'; and have one JSON

let overwrite_key = {
  studentimage: 'student',
  ratingscalename: 'rating-scale-name',
  ratingscale: 'rating-scale-name',
  capturesettingset: 'capture-setting-set',
  capturesetting: 'capture-setting-set',
  eventlog: 'event-log',
  goalcategorie: 'goal-category',
  userimage: 'user',
  datasheetlink: 'datasheet',
  datasheetgoal: 'datasheet',
};
let get_value = overwrite_key.check_permission_key;

So I am fetching the data like this, but it is not working can anyone please help me to resolve this issue?

Answer 1

Objects in javascript are pretty much arrays with String keys, so you can access a dynamic property like this:

let get_value = overwrite_key[check_permission_key];
Answer 2

To access the object property value using a variable use Bracket ([]) notation:

let overwrite_key = {'studentimage':'student','ratingscalename':'rating-scale-name','ratingscale':'rating-scale-name','capturesettingset':'capture-setting-set','capturesetting':'capture-setting-set','eventlog':'event-log','goalcategorie':'goal-category','userimage':'user','datasheetlink':'datasheet','datasheetgoal':'datasheet'}; 
let check_permission_key = 'ratingscalename'; 
let get_value = overwrite_key[check_permission_key]; 
console.log(get_value);

Answer 3

You can use eval() function.

let overwrite_key = { 
  studentimage: 'student', 
  ratingscalename: 'rating-scale-name', 
  ratingscale: 'rating-scale-name', 
  capturesettingset: 'capture-setting-set', 
  capturesetting: 'capture-setting-set', 
  eventlog: 'event-log', 
  goalcategorie: 'goal-category', 
  userimage: 'user', 
  datasheetlink: 'datasheet', 
  datasheetgoal: 'datasheet', 
}; 
let check_permission_key = 'ratingscalename'; 
let get_value = eval(`overwrite_key.${check_permission_key}`); 
console.log(get_value);

JavaScript eval() Function

READ ALSO
how to enable textfield to edit using javascript and html?

how to enable textfield to edit using javascript and html?

I am getting value on my textfield from the database and it works perfectly fine

43
I have create some prepopulated text in a text fiel that is read only however I want to be able to add text after it

I have create some prepopulated text in a text fiel that is read only however I want to be able to add text after it

I have create some prepopulated text in a text field that is read only however I want to be able to add text after it in jqueryAttached works perfectly fine for readonly

35
How to login a specific user to selected activity

How to login a specific user to selected activity

This app can register 2 main users student and teacher after registering the users they can login if the user is a teacher there will be a toast msg saying teacher if it's a student there will be a toast saying student in the register page student and teacher...

51
How can I load an image from URL without extension

How can I load an image from URL without extension

I'd like to load an image from a URL but it doesn't work because the link doesn't have an extension Can this be solved??? URL example : http://d1s5a4e3za7rnicloudfront

33