【问题标题】:JavaScript jQuery-csv. How to trim() headers?JavaScript jQuery-csv。如何修剪()标题?
【发布时间】:2016-10-16 17:02:36
【问题描述】:

我已经实现了 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); 

是否可以 trim() 标题的空白?

【问题讨论】:

  • 你能告诉我们data 变量是什么样的吗?
  • 对象类型。像数据:{client:{name:“name 123”,description:“Descript 123”......问题就在这背后。如果有任何空格,我将无法访问这些标题

标签: javascript jquery csv jquery-csv


【解决方案1】:

基于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;
});

【讨论】:

    猜你喜欢
    • 2016-08-26
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    • 2023-03-08
    • 1970-01-01
    • 2016-12-13
    相关资源
    最近更新 更多