Why am I getting error in row.join() function?

Viewed 154

I'm trying to generate a csv file using the next code.

function generaBlue() {
  var taula = $("#divTaula")[0].childNodes;

  var id = taula[0].getAttribute("id");
  console.log(id.substr(0, id.indexOf("_")));
  let table = $("#" + id.substr(0, id.indexOf("_"))).DataTable();
  let data = table.rows().data();
  let text = "";
  data.map((row) => (text += row.join(";") + "\n"));
  let blob = new Blob([text], { type: "text/csv;charset=utf-8" });
  saveAs(blob, "prova.csv");
}

And row.join(';') is returning this error:

TypeError: row.join is not a function

1 Answers

The problem was that I was receiving an array of objects that do not accept the join function.

function generaBlue() {
  var taula = $("#divTaula")[0].childNodes;

  var id = taula[0].getAttribute("id");
  console.log(id.substr(0, id.indexOf("_")));
  let table = $("#" + id.substr(0, id.indexOf("_"))).DataTable();
  let data = table.rows().data();
 let text = data.reduce((a, c) => a  + Object.values(c).join(";") + "\n", "");
  let blob = new Blob([text], { type: "text/csv;charset=utf-8" });
  saveAs(blob, "prova.csv");
}
Related