Skip to content

Instantly share code, notes, and snippets.

@brito
Created February 28, 2022 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brito/1bbe3459dc81e73e3c4123796a19bdbe to your computer and use it in GitHub Desktop.
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
/**
* 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