Discord
Login
Community
DARK THEME

How can I read a CSV not as String

I Want do put my CSV inside a good datastructure. loadCsv is working, but I don't need a String. I would like to read like line by line of the csv

nevermind, here is my solution

//javascript


function csvToDataStructure(csv) {
  const lines = csv.trim().split('\n');
  const headers = lines[0].split(',');
  const data = [];

  for (let i = 1; i < lines.length; i++) {
    const values = lines[i].split(',');
    const entry = {};

    for (let j = 0; j < headers.length; j++) {
      entry[headers[j].trim()] = values[j].trim();
    }

    data.push(entry);
  }

  return data;
}

code javascript from this page https://hasnode.byrayray.dev/convert-a-csv-to-a-javascript-array-of-objects-the-practical-guide

in MicroScript >> https://microstudio.io/i/Loginus/csvtoarr/

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community