Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the field name dynamically from the json object in javascript?

So this is the data I retrieved from api, and I would like to get more detail data from each of the filed in the object, like Name, Number, Select and so on.
But now the problem is the field name can be changed from the server side, so the ‘Name’ may become Title someday, and the Number can be changed to some other value in the future, so how should I do to specify them dynamically in the code instead of hard coded.
Any help would be appreciated!

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use the function: Object.values() method to get an array of the object’s values.
Use bracket notation to access the value at the specified index.
The Object.values method returns an array of the object’s values.
Example:

const obj = {country: ‘Chile’, name: ‘bobby hadz’};

const firstValue = Object.values(obj)[0];
console.log(firstValue); // 👉️ "Chile"

const firstKey = Object.keys(obj)[0];
console.log(firstKey); // 👉️ "country"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading