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!
>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"
