我已经实现了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);
是否可以修剪标题的空格?
基于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;
});