<p>I know in JavaScript, <em>objects</em> double as hashes, but I have been unable to find a built-in function to get the keys:</p>
<pre><code>var h = {a:'b', c:'d'};
</code></pre>
<p>I want something like</p>
<pre><code>var k = h.keys() ; // k = ['a', 'c'];
</code></pre>
<p>It is simple to write a function myself to iterate over the items and add the keys to an array that I return, but is there a standard cleaner way to do that?</p>
<p>I keep feeling it must be a simple built in function that I missed but I can't find it!</p>

[{'answer_id': 18929, 'author': 'danb', 'author_id': 2031, 'author_profile': 'https://Stackoverflow.com/users/2031', 'pm_score': 4, 'selected': False, 'text': '<p>This is the best you can do, as far as I know...</p>\n\n<pre><code>var keys = [];\nfor (var k in h)keys.push(k);\n</code></pre>\n'}, {'answer_id': 18930, 'author': 'palmsey', 'author_id': 521, 'author_profile': 'https://Stackoverflow.com/users/521', 'pm_score': 3, 'selected': False, 'text': '<p>I believe you can loop through the properties of the object using for/in, so you could do something like this:</p>\n\n<pre><code>function getKeys(h) {\n  Array keys = new Array();\n  for (var key in h)\n    keys.push(key);\n  return keys;\n}\n</code></pre>\n'}, {'answer_id': 18941, 'author': 'AnnanFay', 'author_id': 2118, 'author_profile': 'https://Stackoverflow.com/users/2118', 'pm_score': 6, 'selected': False, 'text': '<p>For production code requiring a large compatibility with client browsers I still suggest <a href="https://stackoverflow.com/questions/18912/how-can-i-find-the-keys-of-a-hash/6921193#6921193">Ivan Nevostruev\'s answer</a> with shim to ensure <code>Object.keys</code> in older browsers. However, it\'s possible to get the exact functionality requested using ECMA\'s new <code>defineProperty</code> feature.</p>\n<p><strong>As of ECMAScript 5 - Object.defineProperty</strong></p>\n<p>As of ECMA5 you can use <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty" rel="nofollow noreferrer"><code>Object.defineProperty()</code></a> to define non-enumerable properties. The <a href="http://kangax.github.io/compat-table/es5/#test-Object.defineProperty" rel="nofollow noreferrer"><strong>current compatibility</strong></a> still has much to be desired, but this should eventually become usable in all browsers. (Specifically note the current incompatibility with IE8!)</p>\n<pre><code>Object.defineProperty(Object.prototype, \'keys\', {\n  value: function keys() {\n    var keys = [];\n    for(var i in this) if (this.hasOwnProperty(i)) {\n      keys.push(i);\n    }\n    return keys;\n  },\n  enumerable: false\n});\n\nvar o = {\n    \'a\': 1,\n    \'b\': 2\n}\n\nfor (var k in o) {\n    console.log(k, o[k])\n}\n\nconsole.log(o.keys())\n\n# OUTPUT\n# &gt; a 1\n# &gt; b 2\n# &gt; [&quot;a&quot;, &quot;b&quot;]\n</code></pre>\n<p>However, since ECMA5 already added <code>Object.keys</code> you might as well use:</p>\n<pre><code>Object.defineProperty(Object.prototype, \'keys\', {\n  value: function keys() {\n    return Object.keys(this);\n  },\n  enumerable: false\n});\n</code></pre>\n<p><strong>Original answer</strong></p>\n<pre><code>Object.prototype.keys = function ()\n{\n  var keys = [];\n  for(var i in this) if (this.hasOwnProperty(i))\n  {\n    keys.push(i);\n  }\n  return keys;\n}\n</code></pre>\n<hr />\n<p><strong>Edit:</strong> Since this answer has been around for a while I\'ll leave the above untouched. Anyone reading this should also read Ivan Nevostruev\'s answer below.</p>\n<p>There\'s no way of making prototype functions non-enumerable which leads to them always turning up in for-in loops that don\'t use <code>hasOwnProperty</code>. I still think this answer would be ideal if extending the prototype of Object wasn\'t so messy.</p>\n'}, {'answer_id': 3325571, 'author': 'Matthew Darwin', 'author_id': 218940, 'author_profile': 'https://Stackoverflow.com/users/218940', 'pm_score': 2, 'selected': False, 'text': '<p>I wanted to use <a href="https://stackoverflow.com/questions/18912/how-can-i-find-the-keys-of-a-hash/18941#18941">AnnanFay\'s answer</a>:</p>\n<pre><code>Object.prototype.keys = function () ...\n</code></pre>\n<p>However, when using it in conjunction with the <a href="https://en.wikipedia.org/wiki/Google_Maps" rel="nofollow noreferrer">Google Maps</a> API v3, Google Maps is non-functional.</p>\n<p>However,</p>\n<pre><code>for (var key in h) ...\n</code></pre>\n<p>works well.</p>\n'}, {'answer_id': 6921193, 'author': 'Ivan Nevostruev', 'author_id': 93988, 'author_profile': 'https://Stackoverflow.com/users/93988', 'pm_score': 9, 'selected': True, 'text': '<p>There is function in modern JavaScript (ECMAScript 5) called <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys" rel="noreferrer"><code>Object.keys</code></a> performing this operation:</p>\n\n<pre><code>var obj = { "a" : 1, "b" : 2, "c" : 3};\nalert(Object.keys(obj)); // will output ["a", "b", "c"]\n</code></pre>\n\n<p>Compatibility details can be found <a href="http://kangax.github.com/es5-compat-table/" rel="noreferrer">here</a>. </p>\n\n<p>On the <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys" rel="noreferrer">Mozilla site</a> there is also a snippet for backward compatibility:</p>\n\n<pre><code>if(!Object.keys) Object.keys = function(o){\n   if (o !== Object(o))\n      throw new TypeError(\'Object.keys called on non-object\');\n   var ret=[],p;\n   for(p in o) if(Object.prototype.hasOwnProperty.call(o,p)) ret.push(p);\n   return ret;\n}\n</code></pre>\n'}, {'answer_id': 7027468, 'author': 'timotti', 'author_id': 751340, 'author_profile': 'https://Stackoverflow.com/users/751340', 'pm_score': 5, 'selected': False, 'text': '<p>You could use <a href="http://documentcloud.github.com/underscore/#keys" rel="nofollow noreferrer">Underscore.js</a>, which is a JavaScript utility library.</p>\n<pre><code>_.keys({one : 1, two : 2, three : 3});\n// =&gt; [&quot;one&quot;, &quot;two&quot;, &quot;three&quot;]\n</code></pre>\n'}, {'answer_id': 9293357, 'author': 'chim', 'author_id': 673282, 'author_profile': 'https://Stackoverflow.com/users/673282', 'pm_score': 3, 'selected': False, 'text': '<p>Using <a href="https://jquery.com/" rel="nofollow noreferrer">jQuery</a>, you can get the keys like this:</p>\n<pre><code>var bobject =  {primary:&quot;red&quot;, bg:&quot;maroon&quot;, hilite:&quot;green&quot;};\nvar keys = [];\n$.each(bobject, function(key, val){ keys.push(key); });\nconsole.log(keys); // [&quot;primary&quot;, &quot;bg&quot;, &quot;hilite&quot;]\n</code></pre>\n<p>Or:</p>\n<pre><code>var bobject =  {primary:&quot;red&quot;, bg:&quot;maroon&quot;, hilite:&quot;green&quot;};\n$.map(bobject, function(v, k){return k;});\n</code></pre>\n<p>Thanks to @pimlottc.</p>\n'}, {'answer_id': 9513780, 'author': 'zeacuss', 'author_id': 312329, 'author_profile': 'https://Stackoverflow.com/users/312329', 'pm_score': 1, 'selected': False, 'text': "<p>If you are trying to get the elements only, but not the functions then this code can help you:</p>\n<pre><code>this.getKeys = function() {\n\n    var keys = new Array();\n    for (var key in this) {\n\n        if (typeof this[key] !== 'function') {\n\n            keys.push(key);\n        }\n    }\n    return keys;\n}\n</code></pre>\n<p>This is part of my implementation of the HashMap and I only want the keys. <code>this</code> is the hashmap object that contains the keys.</p>\n"}, {'answer_id': 18153555, 'author': 'Leticia Santos', 'author_id': 2655009, 'author_profile': 'https://Stackoverflow.com/users/2655009', 'pm_score': 5, 'selected': False, 'text': '<p>You can use <code>Object.keys</code>:</p>\n<pre><code>Object.keys(h)\n</code></pre>\n'}, {'answer_id': 72699782, 'author': 'Mouzam Ali', 'author_id': 7188711, 'author_profile': 'https://Stackoverflow.com/users/7188711', 'pm_score': 1, 'selected': False, 'text': '<p>In Javascript we can use</p>\n<pre><code>Object.keys(h)\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/18912', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/238/']