其實就保證不能有空值,有空值不能跳轉
遍歷數組查找對象中是否有空的屬性值,如果有終止程序運行
牛魔王的故事
2019-03-01 20:11:22
TA貢獻1829條經驗 獲得超7個贊
map
函數不會被 return 給停止,如果非要停止,可使用 throw 來中斷程序。
try { [1,2,3].map(item => {console.log(item); throw null}) } catch(e) { console.log(e) }
或者將 map
換成 for + break
const arr = [1,2,3];for (i = 0; i < arr.length; i++){ console.log(arr[i]); break; }
或者使用 some
替換 map
[[1, 2, 3], [4, '', 6], [7, 8, 9]] .some(item => item.some(item => item === '' ? (() => {console.log('null'); return true})() : (() => {console.log(item); return false})()) )
舉報