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?
3
Answers
If they are a constant length, you can try using a RegExp pattern to match the string
To access the inner objects’ data within the "options" object,
you can use Object.keys() and for…of method to iterate through the keys and access their respective data.
please check below code example:
You can use Object.keys() to iterate through the properties of the "options" object like this: