如何使用javascript将CSV数据转换为配对数组数据?

发布于 2024-09-17 23:41:55 字数 1026 浏览 3 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凹づ凸ル 2024-09-24 23:41:55

您需要使用 .split("\n") 函数分割行,使用它您将得到这个数组

['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']

现在您在 再次迭代这个数组 .split >,。完成后你应该得到这个:

[
    ['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']
]

现在你所要做的就是在两级嵌套的 for 循环中迭代它,并从中构建你想要的对象。

You need to split your lines using .split("\n") function, with it you'll get this array

['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']

Now you iterate this array .split again at ,. You should get this after doing it:

[
    ['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']
]

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文