0

I have implemented jQuery-csv library to parse data from CSV file. My CSV file looks like this:

name, description, client name, client address, client desc
Name 123, Descript 123, client name 123, client address 55, client desc 66
Name 456, Descript 456, client name 55, client address 55, client desc 66

I am using $.csv.toObjects(csv);

var data = $.csv.toArrays(csv); 

Is it possible to trim() whitespace for headers?

Evan Plaice
  • 13,944
  • 6
  • 76
  • 94
Laurynas
  • 972
  • 2
  • 11
  • 24
  • Can you show us what the `data` variable looks like? – Danny Harding Jun 15 '16 at 16:06
  • It's object type. Like data: {client: {name: "name 123", description: "Descript 123" ... The problem is behind this. I can't access these headers if there is any whitespace – Laurynas Jun 15 '16 at 20:04

1 Answers1

0

Based on $.csv.toObjects(csv, options, callback) in doc

Below is my solution for removing the whitespaces (using callback)

$.csv.toObjects(csvd, {}, function(err, data){
    $.each(data, function(index, row){
        $.each(row, function(key, value){
            var newKey = $.trim(key);
            if (typeof value === 'string'){
                data[index][newKey] = $.trim(value);
            }
            if (newKey !== key) {
                delete data[index][key];
            }
        });

    });
    processedData = data;
});
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38