将数据集转换为正确的CSV表示法以使用JavaScript下载

时间:2019-07-03 14:38:31

标签: javascript arrays csv dictionary

我目前有以下数据集:

this.set1 // == [111000, 111000, 110000, 110000, 109000]
this.set2 // == [2.073921204, 2.156188965, 2.210624695, 2.210624695, 2.286842346]
this.set3 // == [527.6497192, 522.3652954, 529.675415, 529.675415, 533.8148804]
this.set4 // == [530.6442261, 524.7432861, 532.2295532, 532.2295532, 536.545166]
this.set5 // == [80.73879242, 80.92513275, 80.95175934, 80.95175934, 80.79203796]

我尝试了以下代码将这些数据转换为所需的CSV数据:

// Reference to pulled-data arrays
let data = [
  this.set1
  this.set2
  this.set3
  this.set4
  this.set5
];

// Convert data arrays to CSV format
const CSVURL = 'data:text/csv;charset=UTF-8,';
let formattedData = data.map(e => e.join(',')).join('\n');
let encodedData = CSVURL + encodeURIComponent(formattedData);

// Generic download CSV function
downloadFile('name.csv', encodedData);

但是,这会输出以下格式的CSV文件:

desc 1

我如何将数据转换为CSV格式?

desc 2

编辑:与其他帖子的相似之处只是部分。请参阅评论以获取完整解决方案。

3 个答案:

答案 0 :(得分:0)

您只需要进行少量修改。

请查看下面的代码段并在其中进行注释:

this.set1 = [111000, 111000, 110000, 110000, 109000];
this.set2 = [2.073921204, 2.156188965, 2.210624695, 2.210624695, 2.286842346];
this.set3 = [527.6497192, 522.3652954, 529.675415, 529.675415, 533.8148804];
this.set4 = [530.6442261, 524.7432861, 532.2295532, 532.2295532, 536.545166];
this.set5 = [80.73879242, 80.92513275, 80.95175934, 80.95175934, 80.79203796];

// select headers of the CSV columns
const data = [
  ['set1', 'set2', 'set3', 'set4', 'set5']
];

// pull arrays by headers
data[0].forEach(h => data.push(this[h]));

// Convert data arrays to CSV format
const CSVURL = 'data:text/csv;charset=UTF-8,';
let formattedData = data.map(e => e.join(',')).join('\n');
let encodedData = CSVURL + encodeURIComponent(formattedData);

// Generic download CSV function
downloadFile('name.csv', encodedData);


function downloadFile(n, d) {
  const a = document.createElement('a');
  a.download = n;
  a.href = d;
  document.querySelector('body').appendChild(a);
  a.click();
}

答案 1 :(得分:0)

利用@Heretic Monkey(用于转置数据)和@codeWonderland(用于添加类别名称)的部分建议,我得出了以下解决方案:

// Reference to pulled-data arrays
let data = [
  this.set1
  this.set2
  this.set3
  this.set4
  this.set5
];

// NEW
// Reference to data array names in same order as aforementioned data array
const dataNames = [
  'set1',
  'set2',
  'set3',
  'set4',
  'set5',
]

// NEW
// Add label to each column
data.forEach((datum, index) => {
  datum.unshift(dataNames[index]);
});

// Convert data arrays to CSV format
const CSVURL = 'data:text/csv;charset=UTF-8,';
let transposedData = data[0].map((col, i) => data.map(row => row[i])); // NEW: TRANSPOSE DATA
let formattedData = transposedData.map(e => e.join(',')).join('\n'); // Modified
let encodedData = CSVURL + encodeURIComponent(formattedData);

// Generic download CSV function
downloadFile('name.csv', encodedData);

还注意到@Kosh Very最近增加了替代列标签,引用为:

// pull arrays by headers
data[0].forEach(h => data.push(this[h])); 

答案 2 :(得分:-1)

您只需在每个标签上添加标签作为第一个元素,就会将其作为标题字段拉出。

// Reference to pulled-data arrays
let data = [
  this.set0
  this.set1
  this.set2
  this.set3
  this.set4
];

let i = 0
for (datum in data) {
    datum.unshift(`set${i}`);
}

// Convert data arrays to CSV format
const CSVURL = 'data:text/csv;charset=UTF-8,';
let formattedData = data.map(e => e.join(',')).join('\n');
let encodedData = CSVURL + encodeURIComponent(formattedData);

// Generic download CSV function
downloadFile('name.csv', encodedData);

此外,索引从0; 3开始