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]
1
  • In your code snippet, obj is not a plain object, but rather an array. Commented Jul 14, 2017 at 20:25

6 Answers 6

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);

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for this.. can be made shorter to [].concat(...obj.map(Object.keys));.
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);

Comments

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);

4 Comments

"Uncaught SyntaxError: Unexpected token {",
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?
@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 :)
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.
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;
},[]))

1 Comment

somebody follow me to down vote all my array question .why?
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);

Comments

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.