首页 > 其他 > 详细

Uncaught TypeError: Cannot read property 'forEach' of undefined

时间:2020-05-30 11:06:34      阅读:66      评论:0      收藏:0      [点我收藏+]

js报错 Cannot read property ‘forEach‘ of undefined

数组未定义或者不存在,解决方法:

if (datas && datas.length) {
    datas.forEach(function (item, index) {
        console.log(index);
    })
}

// 或者

if (!datas || !datas.length) return;
datas.forEach(function (item, index) {
    console.log(index);
})

也可以根据实际情况简写为

(datas || []).forEach(function (item) {

});

Uncaught TypeError: Cannot read property 'forEach' of undefined

原文:https://www.cnblogs.com/sirdong/p/12991225.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!