Using Arrows looks a bit strange due to [0]: /
var obj = [
{ key1: 'value1' },
{ key2: 'value2' },
{ key3: 'value3' },
{ key4: 'value4' }
];
var result = obj.map(x => Object.keys(x)[0]);
console.log(result);
Run codeHide resultAnd if there is a pair of keys with one key, then
obj.map(Object.keys).reduce((a, b) => a.concat(b));
Other methods:
[].concat(...obj.map(Object.keys));
[].concat.apply(this, obj.map(Object.keys));
Or using Lodash (which I use a lot)
_.flatten(obj.map(Object.keys))
source
share