Object.keys()
2016-06-13 15:51
453 查看
The
returns an array of a given object's own enumerable properties, in the same order as that provided by a
(the difference being that a for-in loop enumerates properties in the prototype chain as well).
enumerable properties found directly upon
is the same as that given by looping over the properties of the object manually.
If you want all properties, even not enumerables, see
In ES5, if the argument to this method is not an object (a primitive), then it will cause a
In ES6, a non-object argument will be coerced to an object.
To add compatible
support it, copy the following snippet:
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.
Desktop
Mobile
Enumerability
and ownership of properties
Object.keys()method
returns an array of a given object's own enumerable properties, in the same order as that provided by a
for...inloop
(the difference being that a for-in loop enumerates properties in the prototype chain as well).
Syntax
Object.keys(obj)
Parameters
objThe object whose enumerable own properties are to be returned.
Description
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.
Examples
var arr = ['a', 'b', 'c']; console.log(Object.keys(arr)); // console: ['0', '1', '2'] // array like object var obj = { 0: 'a', 1: 'b', 2: 'c' }; console.log(Object.keys(obj)); // console: ['0', '1', '2'] // array like object with random key ordering var an_obj = { 100: 'a', 2: 'b', 7: 'c' }; console.log(Object.keys(an_obj)); // console: ['2', '7', '100'] // getFoo is property which isn't enumerable var my_obj = Object.create({}, { getFoo: { value: function() { return this.foo; } } }); my_obj.foo = 1; console.log(Object.keys(my_obj)); // console: ['foo']
If you want all properties, even not enumerables, see
Object.getOwnPropertyNames().
Notes
In ES5, if the argument to this method is not an object (a primitive), then it will cause a TypeError.
In ES6, a non-object argument will be coerced to an object.
Object.keys("foo"); // TypeError: "foo" is not an object (ES5 code) Object.keys("foo"); // ["0", "1", "2"] (ES6 code)
Polyfill
To add compatible Object.keyssupport 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 !== '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; }; }()); }
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.
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 5.1 (ECMA-262) The definition of 'Object.keys' in that specification. | Standard | Initial definition. Implemented in JavaScript 1.8.5. |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Object.keys' in that specification. | Standard | |
ECMAScript 2017 Draft (ECMA-262) The definition of 'Object.keys' in that specification. | Draft |
Browser compatibility
Desktop Mobile
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 5 | 4.0 (2.0) | 9 | 12 | 5 |
See also
Enumerabilityand ownership of properties
Object.prototype.propertyIsEnumerable()
Object.create()
Object.getOwnPropertyNames()
Object.values()
Object.entries()
相关文章推荐
- Object.getOwnPropertyNames()
- IOS Dev Intro - Object-C Call C C++
- JdbcTemplate 版本4以上的queryforObject和queryforList的实现
- python django form error:module' object has no attribute '_meta'
- [Effective JavaScript 笔记]第31条:使用Object.getPrototypeOf函数而不要使用__proto__属性
- js(javascript)与ios(Objective-C)相互通信交互
- objective-C之NSDate相关类(四)
- objective-C之NSDate相关类(三)
- Windows下的Objective-C集成开发环境(IDE)
- objective-C之NSDate相关类(二)
- PHP 将json的stdClass Object转成数组array 方法二
- Objective-C --- - UITableView 二 对一进行一些属性补充(梳理总结)
- Object 泛型
- Android Studio安装后运行程序时提示Could not reserve enough space for object?
- 论文笔记《Rich Feature Hierarchies for accurate object detection and semantic segmentation》
- Objective-C仿映客跑车动画
- hive脚本出现Error: java.lang.RuntimeException: Error in configuring object和Caused by: java.lang.IndexOutOfBoundsException: Index: 9, Size: 9
- Swift与Object-C的区别
- objective-C之NSDate相关类(一)
- 网页中通过swfobject插入Flash动画以及设置透明