Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting JavaScript object key list vol. 2

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]
like image 618
Magnus Pääru Avatar asked Jul 14 '17 09:07

Magnus Pääru


1 Answers

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);
like image 138
kind user Avatar answered Oct 13 '22 14:10

kind user