Uncaught TypeError: Cannot read property 'forEach' of undefined


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

});


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM