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

Uncaught TypeError: Cannot read property 'DYMC' of null

程序员文章站 2022-03-27 13:09:14
...
    var arr = [null,{"ADDR":"","BZ":"","CZSJ":"","DYMC":"哈哈","FJH":"","GRMJ":"","GRQ":"","GRQF":0,"KH":"","LHMC":"","QFJE":0,"SFLX":"","TRQF":0,"XQMC":"","YHBH":"","YHLB":"","YHXM":"","ZNJ":0}]
for(var i in arr){
        console.log(arr[i].DYMC);
 }

报错

Uncaught TypeError: Cannot read property 'DYMC' of null
    at 1.html:13

原因数组遍历是因为有null
更改:

 var arr = [null,{"ADDR":"","BZ":"","CZSJ":"","DYMC":"哈哈","FJH":"","GRMJ":"","GRQ":"","GRQF":0,"KH":"","LHMC":"","QFJE":0,"SFLX":"","TRQF":0,"XQMC":"","YHBH":"","YHLB":"","YHXM":"","ZNJ":0}]
        for(var i in arr){
            if(arr[i]!=null){
                console.log(arr[i].DYMC);
            }
        }