How to check if a key exists in an object in javascript

This question already has an answer here:

  • Checking if a key exists in a JavaScript object? 16 answers

  • Use the hasOwnProperty call

    if (!obj.hasOwnProperty(key)) {
    
    }
    

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty


    你可以这样做:

    var key = 'key1';
    if (!('key1' in obj)) {
        ....
    } 
    // or
    if (!(key in obj)) {
    
    }
    
    链接地址: http://www.djcxy.com/p/26630.html

    上一篇: 如何检查ajax是否存在?

    下一篇: 如何检查一个键是否存在于javascript中的对象中