Object.keys() method returns an array of a given object's own enumerable property names , iterated in the same order that a normal loop would.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

句法

Object.keys(obj)
					

参数

obj

The object of which the enumerable's own properties are to be returned.

返回值

An array of strings that represent all the enumerable properties of the given object.

描述

Object.keys() returns an array whose elements are strings corresponding to the enumerable properties found directly upon object . The ordering of the properties is the same as that given by looping over the properties of the object manually.

Polyfill

To add compatible Object.keys support in older environments that do not natively support it, copy the following snippet:

// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
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 !== 'function' && (typeof obj !== 'object' || 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;
    };
  }());
}
					

Please note that the above code includes non-enumerable keys in IE7 (and maybe IE8), when passing in an object from a different window.

For a simple Browser Polyfill, see Javascript - Object.keys Browser Compatibility .

范例

Using Object.keys

// simple array
const arr = ['a', 'b', 'c'];
console.log(Object.keys(arr)); // console: ['0', '1', '2']
// array-like object
const obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.keys(obj)); // console: ['0', '1', '2']
// array-like object with random key ordering
const anObj = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.keys(anObj)); // console: ['2', '7', '100']
// getFoo is a property which isn't enumerable
const myObj = Object.create({}, {
  getFoo: {
    value: function () { return this.foo; }
  }
});
myObj.foo = 1;
console.log(Object.keys(myObj)); // console: ['foo']
					

If you want all properties—including non-enumerables—see Object.getOwnPropertyNames() .

Non-object coercion

In ES5, if the argument to this method is not an object (a primitive), then it will cause a TypeError .

From ES2015 onwards, a non-object argument will be coerced to an object.

// In ES5
Object.keys('foo');  // TypeError: "foo" is not an object
// In ES2015+
Object.keys('foo');  // ["0", "1", "2"]
					

规范

规范
ECMAScript (ECMA-262)
The definition of 'Object.keys' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
keys Chrome 5 Edge 12 Firefox 4 IE 9 Opera 12 Safari 5 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 12 Safari iOS 5 Samsung Internet Android 1.0 nodejs Yes

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: