如何使用javascript将CSV数据转换为配对数组数据?
我正在尝试将 CSV 数据转换为配对数组数据,但我对 javascript 完全陌生,我现在不知道该怎么做。有人有什么想法吗? 提前致谢!
CSV 数据:
year,USA,EU,UK,China,India
2003,10882,10970,1795,1575,599
2002,10383,9040,1564,1434,510
2001,10020,8303,1430,1345,479
2000,9762,8234,1438,1252,457
1999,9213,8901,1460,1158,447
1998,8720,8889,1423,1148,414
对象数组数据:
[{
label: "USA",
data: [[2003, 10882],
[2002, 10383],
[2001, 10020],
[2000, 9762],
[1999, 9213],
[1998, 8720]]
},
{
label: "EU",
data: [[2003, 10970],
[2002, 9040],
[2001, 8303],
[2000, 8234],
[1999, 8901],
[1998, 8889]]
},
{
label: "UK",
data: [[2003, 1795],
[2002, 1564],
[2001, 1430],
[2000, 1438],
[1999, 1460],
[1998, 1423]]
},
{
label: "China",
data: [[2003, 1575],
[2002, 1434],
[2001, 1345],
[2000, 1252],
[1999, 1158],
[1998, 1148]]
},
{
label: "India",
data: [[2003, 599],
[2002, 510],
[2001, 479],
[2000, 457],
[1999, 447],
[1998, 414]]
}]
I'm trying to convert CSV data into paring array data, but I'm totally new to javascript, and I don't know how to do it now. Does any one have some idea?
Thanks in advance!
CSV data:
year,USA,EU,UK,China,India
2003,10882,10970,1795,1575,599
2002,10383,9040,1564,1434,510
2001,10020,8303,1430,1345,479
2000,9762,8234,1438,1252,457
1999,9213,8901,1460,1158,447
1998,8720,8889,1423,1148,414
Object array data:
[{
label: "USA",
data: [[2003, 10882],
[2002, 10383],
[2001, 10020],
[2000, 9762],
[1999, 9213],
[1998, 8720]]
},
{
label: "EU",
data: [[2003, 10970],
[2002, 9040],
[2001, 8303],
[2000, 8234],
[1999, 8901],
[1998, 8889]]
},
{
label: "UK",
data: [[2003, 1795],
[2002, 1564],
[2001, 1430],
[2000, 1438],
[1999, 1460],
[1998, 1423]]
},
{
label: "China",
data: [[2003, 1575],
[2002, 1434],
[2001, 1345],
[2000, 1252],
[1999, 1158],
[1998, 1148]]
},
{
label: "India",
data: [[2003, 599],
[2002, 510],
[2001, 479],
[2000, 457],
[1999, 447],
[1998, 414]]
}]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
.split("\n")
函数分割行,使用它您将得到这个数组现在您在
再次迭代这个数组
.split
>,。完成后你应该得到这个:现在你所要做的就是在两级嵌套的 for 循环中迭代它,并从中构建你想要的对象。
You need to split your lines using
.split("\n")
function, with it you'll get this arrayNow you iterate this array
.split
again at,
. You should get this after doing it:Now all you have to do is to iterate over this in a two level nested for loop, and build the object you want out of it.