How to list the properties of a JavaScript object?

后端 未结 17 2575
刺人心
刺人心 2020-11-22 00:34

Say I create an object thus:

var myObject =
        {\"ircEvent\": \"PRIVMSG\", \"method\": \"newURI\", \"regex\": \"^http://.*\"};

What is

相关标签:
17条回答
  • 2020-11-22 01:29

    Building on the accepted answer.

    If the Object has properties you want to call say .properties() try!

    var keys = Object.keys(myJSONObject);
    
    for (var j=0; j < keys.length; j++) {
      Object[keys[j]].properties();
    }
    
    0 讨论(0)
  • 2020-11-22 01:29

    The solution work on my cases and cross-browser:

    var getKeys = function(obj) {
        var type = typeof  obj;
        var isObjectType = type === 'function' || type === 'object' || !!obj;
    
        // 1
        if(isObjectType) {
            return Object.keys(obj);
        }
    
        // 2
        var keys = [];
        for(var i in obj) {
            if(obj.hasOwnProperty(i)) {
                keys.push(i)
            }
        }
        if(keys.length) {
            return keys;
        }
    
        // 3 - bug for ie9 <
        var hasEnumbug = !{toString: null}.propertyIsEnumerable('toString');
        if(hasEnumbug) {
            var nonEnumerableProps = ['valueOf', 'isPrototypeOf', 'toString',
                'propertyIsEnumerable', 'hasOwnProperty', 'toLocaleString'];
    
            var nonEnumIdx = nonEnumerableProps.length;
    
            while (nonEnumIdx--) {
                var prop = nonEnumerableProps[nonEnumIdx];
                if (Object.prototype.hasOwnProperty.call(obj, prop)) {
                    keys.push(prop);
                }
            }
    
        }
    
        return keys;
    };
    
    0 讨论(0)
  • 2020-11-22 01:34

    As slashnick pointed out, you can use the "for in" construct to iterate over an object for its attribute names. However you'll be iterating over all attribute names in the object's prototype chain. If you want to iterate only over the object's own attributes, you can make use of the Object#hasOwnProperty() method. Thus having the following.

    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            /* useful code here */
        }
    }
    
    0 讨论(0)
  • 2020-11-22 01:36

    Mozilla has full implementation details on how to do it in a browser where it isn't supported, if that helps:

    if (!Object.keys) {
      Object.keys = (function () {
        var hasOwnProperty = Object.prototype.hasOwnProperty,
            hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
            dontEnums = [
              'toString',
              'toLocaleString',
              'valueOf',
              'hasOwnProperty',
              'isPrototypeOf',
              'propertyIsEnumerable',
              'constructor'
            ],
            dontEnumsLength = dontEnums.length;
    
        return function (obj) {
          if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) throw new TypeError('Object.keys called on non-object');
    
          var result = [];
    
          for (var prop in obj) {
            if (hasOwnProperty.call(obj, prop)) result.push(prop);
          }
    
          if (hasDontEnumBug) {
            for (var i=0; i < dontEnumsLength; i++) {
              if (hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i]);
            }
          }
          return result;
        };
      })();
    }
    

    You could include it however you'd like, but possibly in some kind of extensions.js file at the top of your script stack.

    0 讨论(0)
  • 2020-11-22 01:37

    This will work in most browsers, even in IE8 , and no libraries of any sort are required. var i is your key.

    var myJSONObject =  {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}; 
    var keys=[];
    for (var i in myJSONObject ) { keys.push(i); }
    alert(keys);
    
    0 讨论(0)
提交回复
热议问题