【问题标题】:Is there a way to read a csv column when its name contains spaces in Angular? [duplicate]当 csv 列的名称在 Angular 中包含空格时,有没有办法读取它? [复制]
【发布时间】:2020-07-25 07:34:37
【问题描述】:

我使用 d3.js 库在 Angular 中读取 CSV。这是我的阅读方式。

d3.csv('../assets/datasets/tests-per-million.csv').then((data: any) => {
  this.testsData = data;
  for (let i = 0; i < this.testsData.length; i++) {
    this.testCountries.push(this.testsData[i].Entity.split(' ')[0]);
    this.testDates.push(this.testsData[i].Date);
    this.cumulativeTests.push(this.testsData[i].Cumulative);
  }
}); 

问题在于 CSV 的最后一列在其名称中包含空格。这是 CSV 行的示例。

Entity: "Australia - units unclear"
Code: ""
Date: "Mar 22, 2020"
Cumulative total tests per million: "5632.53024026095"

我已经成功阅读了前三列,但我无法捕捉到名称中包含空格的最后一列。

【问题讨论】:

    标签: javascript angular csv


    【解决方案1】:

    由于键 (Cumulative total tests per million) 有空格,因此您需要使用括号表示法访问 JSON:

    this.cumulativeTests.push(this.testsData[i]['Cumulative total tests per million']);

    【讨论】:

      猜你喜欢
      • 2018-11-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      相关资源
      最近更新 更多