当我检查数组的长度时,出现以下错误。正确的方法是什么?

main.js

if (drugPrice.mailPrice.rejectMessage.length !== 0 && Array.isArray(drugPrice.mailPrice.rejectMessage)) {
   //code goes here
}


错误

TypeError: Cannot read property 'length' of undefined

最佳答案

尝试交换支票的顺序:

if (Array.isArray(drugPrice.mailPrice.rejectMessage) && drugPrice.mailPrice.rejectMessage.length !== 0) {
                                        code goes here
}

10-06 00:18