Getting JavaScript object key list

后端 未结 17 1068
野性不改
野性不改 2020-11-22 01:27

I have a JavaScript object like

var obj = {
   key1: \'value1\',
   key2: \'value2\',
   key3: \'value3\',
   key4: \'value4\'
}

How can I

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

    Anurags answer is basically correct. But to support Object.keys(obj) in older browsers as well you can use the code below that is copied from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys . It adds the Object.keys(obj) method if it's not available from the browser.

    if (!Object.keys) {
     Object.keys = (function() {
     'use strict';
     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 = [], prop, i;
    
      for (prop in obj) {
        if (hasOwnProperty.call(obj, prop)) {
          result.push(prop);
        }
      }
    
      if (hasDontEnumBug) {
        for (i = 0; i < dontEnumsLength; i++) {
          if (hasOwnProperty.call(obj, dontEnums[i])) {
            result.push(dontEnums[i]);
          }
        }
      }
      return result;
    };
    }());
    }
    
    0 讨论(0)
  • 2020-11-22 01:59

    Note that in coffeescript this can be accomplished in all browsers and node as

    k for k of obj
    

    and thus

    (1 for _ of obj).length
    
    0 讨论(0)
  • 2020-11-22 02:00

    var obj = {
       key1: 'value1',
       key2: 'value2',
       key3: 'value3',
       key4: 'value4'
    }
    var keys = Object.keys(obj);
    console.log('obj contains ' + keys.length + ' keys: '+  keys);

    It's supported on most major browsers now.

    0 讨论(0)
  • 2020-11-22 02:00

    If you only want the keys which are specific to that particular object and not any derived prototype properties:

    function getKeys(obj) {
        var r = []
        for (var k in obj) {
            if (!obj.hasOwnProperty(k)) 
                continue
            r.push(k)
        }
        return r
    }
    

    e.g:

    var keys = getKeys({'eggs': null, 'spam': true})
    var length = keys.length // access the `length` property as usual for arrays
    
    0 讨论(0)
  • 2020-11-22 02:01

    For a comma-delineated string listing the keys of a JSON Object, try the following:

    function listKeys(jObj){
        var keyString = '';
        for(var k in jObj){
            keyString+=(','+k);
        }
        return keyString.slice(1);
    }
    
    
    
    /* listKeys({'a' : 'foo', 'b' : 'foo', 'c' : 'foo'}) -> 'a,b,c' */
    
    0 讨论(0)
  • 2020-11-22 02:02

    Use Object.keys()... it's the way to go.

    Full documentation is available on the MDN site linked below:

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

    0 讨论(0)
提交回复
热议问题