Home > Back-end >  fetch CSV skipping header
fetch CSV skipping header

Time:11-19

I've a simple function for read a csv file and works good! but i want to skip the header and start to use it from 2nd line

CSV

| date              | open    | high    | close  | low     | volume  |
 ─────────────────── ───────── ───────── ──────── ───────── ───────── 
| 2022-05-15 18:00  | 25.652  | 33.451  | 26.185 | 23.146  | 486.651 |

JS `

const getData = async () => {
    const res   = await fetch('data.csv');
    const resp  = await res.text();
    const cdata = resp.split('\n').map((row) => {
      const [date, open, high, low, close, volume] = row.split(',');
      return {
        date:   date,
        open:   open,
        high:   high,
        low:    low,
        close:  close,
        volume: volume,
      };
    });
    return cdata;
    //   console.log(cdata);
  };

`

CodePudding user response:

Just do .split('\n').slice(1).map((row) => {

const data = `col1,col2
1,2
3,4`

console.log(data.split('\n').slice(1))

  • Related