将CSV数据转换为数组格式 [英] Converting csv data into an array format

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

问题描述

我正在尝试使用jquery形成wordCloud.我有一个要读取的csv文件,并使用该数据形成wordCloud. 我的csv文件中有

I am trying to form a wordCloud using jquery. I have a csv file to be read and use that data to form a wordCloud. I have coloumns in my csv file as

text weight
Lorem 15
Ipsum 9 

,依此类推 但是输入数据必须采用以下格式

and so on But the input data needs to be in the following format

var word_array = [
          {text: "Lorem", weight: 15},
          {text: "Ipsum", weight: 9},
          {text: "Dolor", weight: 6},
          {text: "Sit", weight: 7}
          ];

我应如何将我的csv数据转换为上述格式,以便能够形成单词cloud.请尽可能附上代​​码.我正在html页面中完成所有操作.谢谢.

How should i convert my csv data into the above format to be able to form the word cloud.Please attach the code if possible. I am doing all this in my html page.Thank you.

推荐答案

以下是可能的解决方法:

Here is a possible solution :

var csv = "";
csv += "text weight\n";
csv += "Lorem 15\n";
csv += "Ipsum 9";

var lines = csv.split("\n");
var titles = lines[0].split(" ");
var data = new Array(lines.length - 1);

for (var i = 1; i < lines.length; i++) {
  data[i - 1] = {};
  lines[i] = lines[i].split(" ");
  for (var j = 0; j < titles.length; j++) {
    data[i - 1][titles[j]] = lines[i][j];
  }
}

console.log(data);

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

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