adambuczek
11/30/2017 - 4:14 PM

Table to JSON

Table to JSON

const tabularData = [
  ['First Name', 'Last Name'],
  ['Adam', 'Buczek'],
  ['Jane', 'Doe'],
  ['John', 'Smith']
]

tableToJSON = (table) => {
  const headerValues = tabularData.shift();
  let data = [];
  
  table.forEach(function(row) {
    data.push(row.reduce(function(acc, cur, index) {
      acc[headerValues[index]] = cur;
      return acc;
    }, {}));
  })
  
  return data;
}