Skip to content

Instantly share code, notes, and snippets.

@sscotth
Last active May 1, 2017 23:37
Show Gist options
  • Save sscotth/89d4a562b0d28ee350b960e45f3a8be8 to your computer and use it in GitHub Desktop.
Save sscotth/89d4a562b0d28ee350b960e45f3a8be8 to your computer and use it in GitHub Desktop.
Converts an HTML table to a plain JavaScript Object (POJO)
[...document.querySelectorAll('tr')].slice(1).map(row=>([...document.querySelectorAll('th')].map(el=>el.textContent).reduce((obj,head,i)=>({...obj,[head]:row.querySelectorAll('td')[i].textContent}),{})))
{
const headers = [...document.querySelectorAll('th')].map(el => el.textContent)
const rows = [...document.querySelectorAll('tr')].slice(1)
rows.map(row => {
const rowData = [...row.querySelectorAll('td')].map(el => el.textContent)
return rowData.reduce((obj, item, i) => ({
...obj,
[headers[i]]: item
}), {})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment