欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Uncaught TypeError: Cannot read property ‘forEach‘ of undefined

程序员文章站 2022-06-07 12:35:41
...

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) {

});
相关标签: 微信小程序