JS 判断对象中是否具有指定数据

hasOwnProperty() 方法会返回一个布尔值,指示对象自身属性中是否具有指定的属性(也就是,是否有指定的键)。

语法

obj.hasOwnProperty(prop)    //prop  要检测的属性的 String 字符串形式表示的名称,或者 Symbol返回值  true 或者 false

查看 Demo

    var obj = {"user":"zhangsan","password":"123456"};
    console.log(obj.hasOwnProperty("user"))            //对象中存在user属性 返回true
    console.log(obj.hasOwnProperty("password"))        //对象中存在password属性 返回true  
    console.log(obj.hasOwnProperty("name"))            //对象中不存在name属性 返回false

 

猜你喜欢

转载自www.cnblogs.com/banyuege/p/13379803.html