JavaScript 数组转 CSV [英] JavaScript array to CSV

查看:39
本文介绍了JavaScript 数组转 CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经关注了这篇文章如何将 JavaScript 数组信息导出到 csv(在客户端)? 将嵌套的 js 数组写入 csv 文件.

数组看起来像:

var test_array = [["name1", 2, 3], ["name2", 4, 5], ["name3", 6, 7], ["name4", 8, 9],["name5", 10, 11]];

链接中给出的代码运行良好,只是在 csv 文件的第三行之后,所有其余的值都在同一行例如

名称1,2,3
名称2,4,5
名称3,6,7
name4,8,9name5,10,11 etc 等

谁能解释一下这是为什么?同样使用 Chrome 或 FF.

谢谢

编辑

jsfiddle http://jsfiddle.net/iaingallagher/dJKz6/

伊恩

解决方案

引用的答案是错误的.你必须改变

csvContent += index 

csvContent += dataString + "\n";

至于为什么引用的答案是错误的(搞笑已经被接受了!):indexforEach回调函数的第二个参数,是循环数组,将其与 infoArray 的大小进行比较是没有意义的,infoArray 是所述数组的一个项目(恰好也是一个数组).

编辑

自从我写下这个答案已经过去了六年.许多事情都发生了变化,包括浏览器.以下是答案的一部分:

老化部分的开始

顺便说一句,引用的代码是次优的.您应该避免重复附加到字符串.您应该改为附加到一个数组,并在最后执行一个 array.join("\n") .像这样:

var lineArray = [];data.forEach(函数(信息数组,索引){var line = infoArray.join(",");lineArray.push(index == 0 ? "data:text/csv;charset=utf-8," + line : line);});var csvContent = lineArray.join("\n");

老化部分结束

(请记住,CSV 大小写与通用字符串连接有点不同,因为对于每个字符串,您还必须添加分隔符.)

无论如何,以上似乎不再是正确的,至少对于 Chrome 和 Firefox 来说不是这样(不过对于 Safari 似乎仍然如此).

为了结束不确定性,我编写了一个 jsPerf 测试来测试是否,为了以逗号分隔的方式连接字符串,将它们压入数组并加入数组会更快,或者先用逗号连接它们,然后使用+=运算符直接连接结果字符串.

请点击链接并运行测试,以便我们有足够的数据来谈论事实而不是观点.

I've followed this post How to export JavaScript array info to csv (on client side)? to get a nested js array written as a csv file.

The array looks like:

var test_array = [["name1", 2, 3], ["name2", 4, 5], ["name3", 6, 7], ["name4", 8, 9], ["name5", 10, 11]];

The code given in the link works nicely except that after the third line of the csv file all the rest of the values are on the same line e.g.

name1,2,3
name2,4,5
name3,6,7
name4,8,9name5,10,11 etc etc

Can anyone shed any light on why this is? Same using Chrome or FF.

Thanks

EDIT

jsfiddle http://jsfiddle.net/iaingallagher/dJKz6/

Iain

解决方案

The cited answer was wrong. You had to change

csvContent += index < infoArray.length ? dataString+ "\n" : dataString;

to

csvContent += dataString + "\n";

As to why the cited answer was wrong (funny it has been accepted!): index, the second parameter of the forEach callback function, is the index in the looped-upon array, and it makes no sense to compare this to the size of infoArray, which is an item of said array (which happens to be an array too).

EDIT

Six years have passed now since I wrote this answer. Many things have changed, including browsers. The following was part of the answer:

START of aged part

BTW, the cited code is suboptimal. You should avoid to repeatedly append to a string. You should append to an array instead, and do an array.join("\n") at the end. Like this:

var lineArray = [];
data.forEach(function (infoArray, index) {
    var line = infoArray.join(",");
    lineArray.push(index == 0 ? "data:text/csv;charset=utf-8," + line : line);
});
var csvContent = lineArray.join("\n");

END of aged part

(Keep in mind that the CSV case is a bit different from generic string concatenation, since for every string you also have to add the separator.)

Anyway, the above seems not to be true anymore, at least not for Chrome and Firefox (it seems to still be true for Safari, though).

To put an end to uncertainty, I wrote a jsPerf test that tests whether, in order to concatenate strings in a comma-separated way, it's faster to push them onto an array and join the array, or to concatenate them first with the comma, and then directly with the result string using the += operator.

Please follow the link and run the test, so that we have enough data to be able to talk about facts instead of opinions.

这篇关于JavaScript 数组转 CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆