i have a question, i have a 'CSV file' data and it looks like this
我有一个问题,我有一个'CSV文件'数据,它看起来像这样
source,xPos,target,xPos,value
user1,0,user2,1,4
user2,1,user3,2,4
user1,0,user3,2,2
user4,2
userA1,1,userA2,2,4
and I need to reform it, because my fuction uses a data set variable with in this format which is in somehow a JSON format and it should look like :
我需要改进它,因为我的函数使用了这种格式的数据集变量,它在某种程度上是一种JSON格式,它看起来像:
{
"nodes":[
{"node":0,"name":"user1","xPos":0},
{"node":1,"name":"user2","xPos":1},
{"node":2,"name":"user3","xPos":2},
{"node":3,"name":"user4","xPos":2},
{"node":4,"name":"userA1","xPos":1}
{"node":5,"name":"userA2","xPos":2}
],
"links":[
{"source":0,"target":1,"value":4},
{"source":1,"target":2,"value":4},
{"source":0,"target":2,"value":2},
{"source":4,"target":5,"value":4}]
}
and the sources and targets are the node numbers that they are already predefined.
并且源和目标是它们已经预定义的节点号。
Is it possible to do that in javascript ?
有可能在javascript中这样做吗?
2 个解决方案
#1
0
First split the csv data by lines:
首先按行拆分csv数据:
var lines = csvData.split("\n");
Then for each line split the line by comma (,)
然后为每一行用逗号分隔行(,)
var dataLine = lines[1].split(","); //not the 0th line as it has only the headers
Then loop dataLine
with for or forEach
and organize the data the way you want. Declare two json arrays one for "links"
and other for "nodes"
. Then assign your organized data to them.
然后使用for或forEach循环dataLine并按照您希望的方式组织数据。声明两个json数组一个用于“链接”,另一个用于“节点”。然后将有组织的数据分配给他们。
And this is one of the way it is to be done.
这是它的一种方式。
#2
2
Yes it is possible to do that in Javascript.
是的,可以在Javascript中执行此操作。
#1
0
First split the csv data by lines:
首先按行拆分csv数据:
var lines = csvData.split("\n");
Then for each line split the line by comma (,)
然后为每一行用逗号分隔行(,)
var dataLine = lines[1].split(","); //not the 0th line as it has only the headers
Then loop dataLine
with for or forEach
and organize the data the way you want. Declare two json arrays one for "links"
and other for "nodes"
. Then assign your organized data to them.
然后使用for或forEach循环dataLine并按照您希望的方式组织数据。声明两个json数组一个用于“链接”,另一个用于“节点”。然后将有组织的数据分配给他们。
And this is one of the way it is to be done.
这是它的一种方式。
#2
2
Yes it is possible to do that in Javascript.
是的,可以在Javascript中执行此操作。