Checking if a key exists in a JavaScript object?

前端 未结 22 2208
礼貌的吻别
礼貌的吻别 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:32

    The accepted answer refers to Object. Beware using the in operator on Array to find data instead of keys:

    ("true" in ["true", "false"])
    // -> false (Because the keys of the above Array are actually 0 and 1)
    

    To test existing elements in an Array: Best way to find if an item is in a JavaScript array?

提交回复
热议问题