I have this following object
{
"Monday": [
{
"morning": [
{
"start_time": "02:00",
"end_time": "07:30"
}
],
"afternoon": [
{
"start_time": "02:00",
"end_time": "05:00"
}
],
"evening": [
{
"start_time": "02:30",
"end_time": "07:00"
}
]
}
],
"Tuesday": [
{
"morning": [
{
"start_time": "02:00",
"end_time": "07:30"
}
],
"afternoon": [
{
"start_time": "02:00",
"end_time": "05:00"
}
],
"evening": [
{
"start_time": "02:30",
"end_time": "07:00"
}
]
}
],
..
}
i want to loop through all object keys and values
for (var prop in this.jsonData) {
console.log("Key:" + prop);
console.log("Value:" + this.jsonData[prop]);
}
getting
Key:Monday value : undefined
but i need to access inner object values.
2
Answers
You will need to loop through the inner arrays as well to access the inner object values. Basically, this can be achieved using:
Object.entries()
to get an array of key-value pairs for the outer and inner objects.Edit:
You need to typecast the
slots
explicitly. Something like:Typescript playground
Nested Objects Accessing
As we can see the object structure is rather complex, we need to go deep inside all the subobjects, i.e. day and then to time.
To do that, we can write a function:
Ps: Instead of logging everything, you can add a search key, and find a particular day or time, and you can also save those values somewhere too.