Revision 15251 of Object.keys()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/keys
  • Revision title: keys
  • Revision id: 15251
  • Created:
  • Creator: Inimino
  • Is current revision? No
  • Comment replace compatibility snippet with a version that will not choke on Object.keys({hasOwnProperty:0}); 44 words added, 17 words removed

Revision Content

{{ js_minversion_header("1.8.5") }}

This method is compliant with ECMAScript 5.

Summary

Returns an array of all own enumerable properties found upon a given object, in the same order as that provided by a for-in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

Syntax

var props = Object.keys(obj);

Parameters

obj
The 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"]; alert(Object.keys(arr)); // will alert "0,1,2"

// array like object
var obj = { 0 : "a", 1 : "b", 2 : "c"};
alert(Object.keys(obj)); // will alert "0,1,2"

// getFoo is property which isn't enumerable
var my_obj = Object.create({}, { getFoo : { value : function () { return this.foo } } });
my_obj.foo = 1;

alert(Object.keys(my_obj)); // will alert only foo

If you want all properties, even the not enumerable, see Object.getOwnPropertyNames

Compatiblity

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

if(!Object.keys) Object.keys = function(o){var ret=[],p;
 for(p in o) if(Object.prototype.hasOwnProperty.call(o,p)) ret.push(p);
 return ret;
}

See also

Revision Source

<p>{{ js_minversion_header("1.8.5") }}</p>
<p>This method is compliant with ECMAScript 5.</p>
<h3 name="Summary">Summary</h3>
<p>Returns an array of all own enumerable properties found upon a given object, in the same order as that provided by a <a href="/en/JavaScript/Reference/Statements/for...in" title="en/JavaScript/Reference/Statements/for...in"><code>for-in</code></a> loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).</p>
<h3 name="Syntax">Syntax</h3>
<p><code>var props = Object.keys(<em>obj</em>);</code></p>
<h3 name="Parameters">Parameters</h3>
<dl> <dt>obj</dt> <dd>The object whose enumerable own properties are to be returned.</dd>
</dl>
<h3 name="Description">Description</h3>
<p><code>Object.keys</code> returns an array whose elements are strings corresponding to the enumerable properties found directly upon <code>object</code>. The ordering of the properties is the same as that given by looping over the properties of the object manually.</p>
<h3>Examples</h3>
<pre class="brush: js"><code>var arr = ["a", "b", "c"]; alert(Object.keys(arr));</code> // will alert "0,1,2"

// array like object
var obj = { 0 : "a", 1 : "b", 2 : "c"};
alert(Object.keys(obj)); // will alert "0,1,2"

// getFoo is property which isn't enumerable
var my_obj = Object.create({}, { getFoo : { value : function () { return this.foo } } });
my_obj.foo = 1;

alert(Object.keys(my_obj)); // will alert only foo
</pre>
<p>If you want all properties, even the not enumerable, see <a href="/en/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames" title="en/Core JavaScript 1.5 Reference/Global Objects/Object/getOwnPropertyNames">Object.getOwnPropertyNames</a>. </p>
<h3 name="See_also">Compatiblity</h3>
<p>To add compatible Object.keys support in older environments that do not natively support it, copy the following snippet:</p>
<pre class="brush: js">if(!Object.keys) Object.keys = function(o){var ret=[],p;
 for(p in o) if(Object.prototype.hasOwnProperty.call(o,p)) ret.push(p);
 return ret;
}
</pre>
<h3 name="See_also">See also</h3>
<ul> <li><a href="/en/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable" title="en/Core JavaScript 1.5 Reference/Global Objects/Object/propertyIsEnumerable">propertyIsEnumerable</a></li> <li><a href="/en/JavaScript/Reference/Global_Objects/Object/create" title="en/Core JavaScript 1.5 Reference/Global Objects/Object/create">Object.create</a></li> <li><a href="/en/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames" title="en/Core JavaScript 1.5 Reference/Global Objects/Object/getOwnPropertyNames">Object.getOwnPropertyNames</a></li> <li><a class=" link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=307791">Bug 307791</a> -<span id="summary_alias_container"> <span id="short_desc_nonedit_display">Implement ES5's Object.keys(O)</span> </span></li>
</ul>
Revert to this revision