I am wondering what is a better approach to extract all the keys and values of objects in an array as separate arrays. My approach requires me to flatten the array after extracting keys and values of the object by mapping over the array.
Input
const input = [{"a": 1, "b": 2}, {"c": 3}, {"d": 4}, {}, {"e": null, "f": 6, "g": 7}];
Output
const keys = ["a", "b", "c", "d", "e", "f", "g"];
const values = [1, 2, 3, 4, null, 6, 7];
My Solution
const input = [{
"a": 1,
"b": 2
}, {
"c": 3
}, {
"d": 4
}, {}, {
"e": null,
"f": 6,
"g": 7
}];
const keys = input.map(obj => [].concat(Object.keys(obj))).flat();
console.log(keys);
const values = input.map(obj => [].concat(Object.values(obj))).flat();
console.log(values);