判断对象中是否含有此属性hasOwnProperty

对象.hasOwnProperty(“属性名”) 返回一个布尔值,判断对象是否包含特定的自身(非继承)属性。但是不会去查找它的原型链

——示例——

var newObjec = {
    shipId: 45,
    shipName: "CMA CGM MOZART",
    mmsi: "226325000",
    etd: "2020-02-21 00:00",
    eta: "2020-04-18 00:00",
    ppp:{
        a:'a',
        b:'b',
        c:'c'
    },
    ports: [
        {
            portName: "BRISBANE",
            portNameCn: "布里斯班",
            portCode: "",
            terminal: "",
            duration: "",
            eta: "",
            etd: "",
            lastEta: "",
            lastEtd: "",
            lat: -27.47127,
            lon: 153.0149,
        },
        {
            portName: "SHANGHAI",
            portNameCn: "上海",
            portCode: "CNSHA",
            terminal: "外港",
            duration: "",
            eta: "",
            etd: "2020-02-21 00:00",
            lastEta: "",
            lastEtd: "2020-01-08 00:00",
            lat: 31.292088,
            lon: 121.495623,
        }
    ]
}
console.log(newObjec.hasOwnProperty('detail')) /false/
console.log(newObjec.hasOwnProperty('ports')) /true/
console.log(newObjec.hasOwnProperty('shipId')) /true/
console.log((newObjec.ppp).hasOwnProperty('a')) /true/
发布了17 篇原创文章 · 获赞 0 · 访问量 188

猜你喜欢

转载自blog.csdn.net/who_become_gods/article/details/104406296