I would like to get the keys of a JavaScript object as an array, either in jQuery or pure JavaScript.
Is there a less verbose way than this?
var foo = { 'alpha' : 'puffin', 'beta' : 'beagle' };
var keys = [];
for (var key in foo) {
keys.push(key);
}
This question is related to
javascript
ecmascript-5
If you decide to use Underscore.js you better do
var foo = { 'alpha' : 'puffin', 'beta' : 'beagle' };
var keys = [];
_.each( foo, function( val, key ) {
keys.push(key);
});
console.log(keys);
I don't know about less verbose but I was inspired to coerce the following onto one line by the one-liner request, don't know how Pythonic it is though ;)
var keys = (function(o){var ks=[]; for(var k in o) ks.push(k); return ks})(foo);
Of course, Object.keys()
is the best way to get an Object's keys. If it's not available in your environment, it can be trivially shimmed using code such as in your example (except you'd need to take into account your loop will iterate over all properties up the prototype chain, unlike Object.keys()
's behaviour).
However, your example code...
var foo = { 'alpha' : 'puffin', 'beta' : 'beagle' };
var keys = [];
for (var key in foo) {
keys.push(key);
}
...could be modified. You can do the assignment right in the variable part.
var foo = { 'alpha' : 'puffin', 'beta' : 'beagle' };
var keys = [], i = 0;
for (keys[i++] in foo) {}
Of course, this behaviour is different to what Object.keys()
actually does (jsFiddle). You could simply use the shim on the MDN documentation.
In case you're here looking for something to list the keys of an n-depth nested object as a flat array:
const getObjectKeys = (obj, prefix = '') => {_x000D_
return Object.entries(obj).reduce((collector, [key, val]) => {_x000D_
const newKeys = [ ...collector, prefix ? `${prefix}.${key}` : key ]_x000D_
if (Object.prototype.toString.call(val) === '[object Object]') {_x000D_
const newPrefix = prefix ? `${prefix}.${key}` : key_x000D_
const otherKeys = getObjectKeys(val, newPrefix)_x000D_
return [ ...newKeys, ...otherKeys ]_x000D_
}_x000D_
return newKeys_x000D_
}, [])_x000D_
}_x000D_
_x000D_
console.log(getObjectKeys({a: 1, b: 2, c: { d: 3, e: { f: 4 }}}))
_x000D_
You can use jQuery's $.map
.
var foo = { 'alpha' : 'puffin', 'beta' : 'beagle' },
keys = $.map(foo, function(v, i){
return i;
});
For getting all of the keys of an Object you can use Object.keys()
. Object.keys()
takes an object as an argument and returns an array of all the keys.
const object = {_x000D_
a: 'string1',_x000D_
b: 42,_x000D_
c: 34_x000D_
};_x000D_
_x000D_
const keys = Object.keys(object)_x000D_
_x000D_
console.log(keys);_x000D_
_x000D_
console.log(keys.length) // we can easily access the total amount of properties the object has
_x000D_
In the above example we store an array of keys in the keys const. We then can easily access the amount of properties on the object by checking the length of the keys array.
Object.values()
The complementary function of Object.keys()
is Object.values()
. This function takes an object as an argument and returns an array of values. For example:
const object = {_x000D_
a: 'random',_x000D_
b: 22,_x000D_
c: true_x000D_
};_x000D_
_x000D_
_x000D_
console.log(Object.values(object));
_x000D_
Source: Stackoverflow.com