[英]How can i save data from a .txt file to an array in JavaScript?
I have this file , " File.txt " formatted as a big array[ ]
, in each index of this array placed in the .txt file there is a json i want to use later. 我有一个格式为big array[ ]
文件“ File.txt”,在.txt文件中放置的该数组的每个索引中,都有一个我想稍后使用的json 。 I need to read the file , save each json and push it in an array using javascript . 我需要读取文件,保存每个json并使用javascript将其推送到数组中。 Every { content }
, { content 2}
represents a json . 每个{ content }
, { content 2}
代表一个json 。
File.txt File.txt
[
{
"nodes": [
{"id": "Source","label":"name","group": 0},
{"id": "Parent_1","label":"name","group": 1},
{"id": "Parent_2","label":"name","group": 1},
{"id": "Parent_3","label":"name","group": 1},
{"id": "Child_1","label":"name","group": 2},
{"id": "Child_2","label":"name","group": 2},
{"id": "Child_3","label":"name","group": 2},
{"id": "Child_4","label":"name", "group": 3}
],
"links": [
{ "source": "Source","target": "Parent_1"},
{ "source": "Source","target": "Parent_2"},
{ "source": "Source","target": "Parent_3"},
{ "source": "Source","target": "Child_1"},
{ "source": "Source","target": "Child_2"},
{ "source": "Source","target": "Child_3"},
{ "source": "Child_2","target": "Child_4"}
]
} ,
{
"nodes": [
{"id": "Source","label":"name","group": 0},
{"id": "Parent_1","label":"name","group": 1},
{"id": "Parent_2","label":"name","group": 1},
{"id": "Parent_3","label":"name","group": 1},
{"id": "Child_1","label":"name","group": 2},
{"id": "Child_2","label":"name","group": 2},
{"id": "Child_3","label":"name","group": 2},
{"id": "Child_4","label":"name", "group": 3}
],
"links": [
{ "source": "Source","target": "Parent_1"},
{ "source": "Source","target": "Parent_2"},
{ "source": "Source","target": "Parent_3"},
{ "source": "Source","target": "Child_1"},
{ "source": "Source","target": "Child_2"},
{ "source": "Source","target": "Child_3"},
{ "source": "Child_2","target": "Child_4"}
]
}
]
I have in mind something like this : 我想到了这样的事情:
//The array i want to save all the data in
NewArray=[];
//Get the file name
var File = document.getElementById('File.txt');
//Make a loop so i can read each index of the file and save the content in the new array
for (i = 0; i < array.length; i++) {
NewArray[i] = "File.[i] ;
}
One way to make it work is to edit your text file, and prefix its contents with 一种使其工作的方法是编辑文本文件,并在其内容前添加前缀
var data =
Then load it via: 然后通过以下方式加载它:
<script src="file.txt"></script>
Once you have that, you can access the data via the data
variable. 一旦有了它,就可以通过data
变量访问data
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.