6

Here is original question: Getting JavaScript object key list

But if the situation is little bit more complex like:

var obj = [
   { key1: 'value1' },
   { key2: 'value2' },
   { key3: 'value3' },
   { key4: 'value4' }
]

Then how you get keys like that?

[key1,key2,key3,key4]
kind user
  • 40,029
  • 7
  • 67
  • 77

6 Answers6

15

You can use mix of Object.keys and Array#flatMap.

let obj = [
   { key1: 'value1' },
   { key2: 'value2' },
   { key3: 'value3' },
   { key4: 'value4' },
];

let keys = obj.flatMap(Object.keys);

console.log(keys);
kind user
  • 40,029
  • 7
  • 67
  • 77
7

you can get keys for each object in obj and then use concat() to add it to your result array.

    var obj = [
       {
           key1: 'value1'
       },
       {
           key2: 'value2'
       },
       {   
           key3: 'value3'
       },
       {   
           key4: 'value4'
       }
    ]
var keyList = [];
obj.forEach(function(o){
    keyList = keyList.concat(Object.keys(o));
});
console.log(keyList);
Dij
  • 9,761
  • 4
  • 18
  • 35
5

You can achieve it using Array#reduce method.

var obj = [{
  key1: 'value1'
}, {
  key2: 'value2'
}, {
  key3: 'value3'
}, {
  key4: 'value4'
}];

var res = obj
  // iterate over the array
  .reduce(function(arr, o) {
    // get keys and push into the array
    [].push.apply(arr, Object.keys(o));
    // return the araray reference
    return arr;
    // set initial value as an empty array
  }, []);

console.log(res);

// ES6 alternative
var res1 = obj.reduce((arr, o) => (arr.push(...Object.keys(o)), arr), []);

console.log(res1);
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
  • `"Uncaught SyntaxError: Unexpected token {",` – Cerbrus Jul 14 '17 at 09:10
  • It looks strange to me to see `arr.push` mutating the array inside a `reduce`. Why not return `[...arr, Object.keys(o)]`, or do the same with `concat`? – Tom Fenech Jul 14 '17 at 15:27
  • @TomFenech : which generates new array all the time.... in that way `map()` method is better than `reduce()` ,.... That may be better I'm not sure about it :) – Pranav C Balan Jul 14 '17 at 15:43
  • Yes, creating new objects incurs a (very slight) performance penalty, but if you're just gonna push to an array, you may as well use a `for` loop. – Tom Fenech Jul 14 '17 at 15:48
1

Try with Array#reduce() and Object.keys() .Reduce create the array with in function and forEach iterate the inner Object keys

var obj = [ { key1: 'value1' }, { key2: 'value2' }, { 
key3: 'value3' }, { 
key4: 'value4' } ]


console.log(obj.reduce(function(a,b){
 Object.keys(b).forEach(function(val){
   a.push(val)
  })
  return a;
},[]))
prasanth
  • 22,145
  • 4
  • 29
  • 53
1

You can iterate over object properties with for in, something like this.

var obj = [
   { key1: 'value1' },
   { key2: 'value2' },
   { key3: 'value3' },
   { key4: 'value4' }
],
keysArray = [];

obj.forEach((item)=>{
    for(key in item){
      keysArray.push(key);
    }
});
    
console.log(keysArray);
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
AlexanderT
  • 167
  • 10
0

Using Arrows, looks bit odd because of [0] :/

var obj = [
   { key1: 'value1' },
   { key2: 'value2' },
   { key3: 'value3' },
   { key4: 'value4' }
];

var result = obj.map(x => Object.keys(x)[0]);

console.log(result);

And if there are more than one key value pair, then

obj.map(Object.keys).reduce((a, b) => a.concat(b));

Other ways:

[].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))
Mr_Green
  • 40,727
  • 45
  • 159
  • 271