I have a nested object where the inner objects have random serial numbers as their labels.
If I have data like this:
const user = {
id: 101,
email: '[email protected]',
personalInfo: {
name: 'Jack',
address: {
line1: 'westwish st',
line2: 'washmasher',
city: 'wallas',
state: 'WX'
}
}
}
I can access the inner object with
const name = user.personalInfo.name;
const userCity = user.personalInfo.address.city;
But my inner objects have random serial numbers, not static labels like address
as in the above example.
{
"id": "48121be5c6b55ae23928316f5",
"component": "select",
"customFields": [
],
"index": 0,
"label": "Select",
"description": null,
"placeholder": null,
"editable": true,
"options": {
"f1c7363b085cd671a59": {
"index": 0,
"value": "AA",
"label": "A"
},
"3d8508fb86c6af84637": {
"index": 1,
"value": "BB",
"label": "B"
},
"615b8ba2c0b7e88b8b5": {
"index": 2,
"value": "DD",
"label": "D"
}
},
"required": false,
"validation": "/.*/",
"imported": false
}
How do I access the value
for each inner object of the options
object?
You can use Object.keys() to iterate through the properties of the "options" object like this:
const optionKeys = Object.keys(data.options);
optionKeys.forEach(key => {
const innerObject = data.options[key];
console.log(innerObject.value); // Access the "value" property of each inner object
});