首页 > 解决方案 > Javascript数组显示不是数组错误

问题描述

我在另一个对象中有一个对象数组。当我用

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

});

它显示错误 Uncaught TypeError: t.schedules.forEach is not a function。

但我可以用 t.schedules[index];

请看这张图片

带索引

在此处输入图像描述

标签: javascriptjquery

解决方案


像这样试试。If t.schedulesis an objectthen将从 thatObject.values(t.schedules)返回。然后你可以使用array of valuesobjectforEach

if (typeof t.schedules == "object") {
    t.schedules = Object.values(t.schedules);
}
t.schedules.forEach(function(item) {
    console.log(item[index]);
});

推荐阅读