Skip to content

Instantly share code, notes, and snippets.

@UniBreakfast
Last active May 3, 2019 14:42
Show Gist options
  • Save UniBreakfast/8510e5b2f7a5dc0471399356d0609878 to your computer and use it in GitHub Desktop.
Save UniBreakfast/8510e5b2f7a5dc0471399356d0609878 to your computer and use it in GitHub Desktop.
a function to show data arrays as objects (w headers) in console.table
function recordsFrom(data) {
try { var [headers, rows] = data }
catch { var {headers, rows} = data }
return rows.map(row => {
let obj = {}
row.forEach((value,i) => obj[headers[i]] = value)
return obj
})
}
/*
function recordsFrom(data) serves the purpose of making a structure like this one:
[
{ a: 4, b: 12, c: 55 },
{ a: 14, b: 42, c: 57 },
{ a: 40, b: 2, c: 81 }
]
from the structure like in the data variable below, which is needed in order console.table to show it correctly.
*/
data = {
headers: ['a', 'b', 'c'],
rows: [[4, 12, 55], [14, 42, 57], [40, 2, 81]]
}
console.table(recordsFrom(data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment