Using PapaParse transformHeader to remove whitespace from headers?
Asked Answered
H

1

8

If we have a CSV file like this:

`firstName, lastName
 Jim, Carrey
 Stephen, Colbert`

Papaparse will output the dynamically typed result like this:

       {firstName: 'Jim',
       ' lastName': '30000'}

In order to just get {firstName: 'Jim', lastName...} PapaParse 5.0.0 has a transformHeaders option, but there's no documentation on how to achieve this result yet.

Anyone have a transformHeader snippet that does this?

Hardheaded answered 6/6, 2019 at 4:2 Comment(0)
H
20

This ended up doing the trick:

const config = {
  delimiter: ",",
  header: true,
  dynamicTyping: true,
  transformHeader:function(h) {
    return h.trim();
  }
};
Hardheaded answered 6/6, 2019 at 4:9 Comment(3)
I used this syntax instead: transformHeader: ((header: string) => { return ... }Incommensurate
If you want to remove the space from something like First Name then you can use transformHeader:function(h) { return h.replace(/\s/g, ''); }Prothonotary
transformHeader: ((header: string) => header.trim())Tortuosity

© 2022 - 2024 — McMap. All rights reserved.