Created
February 28, 2022 19:19
-
-
Save brito/1bbe3459dc81e73e3c4123796a19bdbe to your computer and use it in GitHub Desktop.
Flatten an Object into a table with rows of columns including keys as headers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Flatten an Object into a table with rows of columns including keys as headers | |
* @param object to flatten | |
*/ | |
function _asTable (object) { | |
return [].concat(object).reduce((list, item, i) => { | |
let labels = Object.keys(item) | |
if (!i && +labels[0] !== 0) list.push(labels) | |
list.push(labels.map(label => item[label])) | |
return list | |
}, []) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment