JavaScript jQuery-csv。如何修剪()标题?

时间:2016-06-15 14:07:11

标签: javascript jquery csv jquery-csv

我已经实施了jQuery-csv库来解析CSV文件中的数据。我的CSV文件如下所示:

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

我正在使用$ .csv.toObjects(csv);

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

是否可以为标题修剪()空格?

1 个答案:

答案 0 :(得分:0)

基于doc

中的$.csv.toObjects(csv, options, callback)

以下是我删除空格的解决方案(使用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;
});