Pivotting SQL Table Response to JSON in JavaScript
Whenever you have code that sets up an array, then has a loop to push items into that array, Array.map() could be used to condense that code. For example, these lines:


const jsonData = [];
data.forEach( (row) =&gt; {
  const json = {};
  row.forEach( (item, index) =&gt; {
    json[columns[index]] = item;
  });
  jsonData.push(json);
});
return jsonData;



Could be simplified to this:

return data.map( (row) =&gt; {
  const json = {};
  row.forEach( (item, index) =&gt; {
    json[columns[index]] = item;
  });
  return json;
});


You could optionally simplify the inner loop using Array.reduce()

return data.map( (row) =&gt; {
  return row.reduce( (json, item, index) =&gt; {
    json[columns[index]] = item;
    return json;
  }, {});
});


There are some good exercises in Functional Programming in Javascript where you practice implementing some of those methods.