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