Created
January 6, 2017 15:15
-
-
Save jwbenson/7fad0fbc1c5b561593067cb64448e95c to your computer and use it in GitHub Desktop.
Object Array to Markdown Table
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
function objectArrayToMarkdown(arr) { | |
var keys = Object.keys(arr[0]); | |
arr.splice(0, 0, Object.assign({}, ...keys.map(key => ({ [key]: key })))); | |
arr.splice(1, 0, Object.assign({}, ...keys.map(key => ({ [key]: '---' })))); | |
return arr.map(row => { | |
return '| ' + (Array.isArray(row) ? row.join(' | ') : keys.map(key => row[key]).join(' | ')) + ' | '; | |
}).join('\n'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment