0

i have an array of object inside another object. when i iterate this array with

t.schedules.forEach(function(item) {
    console.log(item[index]);

});

it shows error Uncaught TypeError: t.schedules.forEach is not a function.

but i am able to get it with t.schedules[index];

please see this image

with index

enter image description here

Neha
  • 2,136
  • 5
  • 21
  • 50

2 Answers2

1

Try like this. If t.schedules is an object then Object.values(t.schedules) will return array of values from that object. Then you can use forEach

if (typeof t.schedules == "object") {
    t.schedules = Object.values(t.schedules);
}
t.schedules.forEach(function(item) {
    console.log(item[index]);
});
Karan
  • 12,059
  • 3
  • 24
  • 40
0

This sort of thing often happens if you try iterating before the array is populated. The logged object looks like a database entry judging by the "created_at", "updated_at" etc. keys so I'm guessing you are retrieving the array from a database? If so, check that you are waiting for the response before iterating.

Code_Frank
  • 381
  • 1
  • 6