Checking if a key exists in a JavaScript object?

前端 未结 22 2654
礼貌的吻别
礼貌的吻别 2020-11-21 22:57

How do I check if a particular key exists in a JavaScript object or array?

If a key doesn\'t exist, and I try to access it, will it return false? Or throw an error?<

22条回答
  •  不要未来只要你来
    2020-11-21 23:35

    vanila js

    yourObjName.hasOwnProperty(key) : true ? false;
    

    If you want to check if the object has at least one property in es2015

    Object.keys(yourObjName).length : true ? false
    

提交回复
热议问题