简体   繁体   English

如何将数据从.txt文件保存到JavaScript中的数组?

[英]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] ;
}

Do it like this 像这样做

$(document).ready(function() {
    $("#lesen").click(function() {
        $.ajax({
            url : "helloworld.txt", //get the file from local/server 
            dataType: "text",
            success : function (data) {
                var arrData = data; //data are in the form of array with json data
            }
        });
    });
}); 

数据将像这样显示

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.

相关问题 如何保存txt文件中的数据返回函数之外的数组? - How to save the data from a txt file returns to an array that is outside the function? 如何从 textarea 的值中保存 a.txt 文件? - How can I save a .txt file from the value of a textarea? 如何从 txt 文件数据(javascript)创建数组(或矩阵)数组 - How to create a array of arrays (or matrix) from a txt file data (javascript) 将数据从.txt文件发送到JavaScript数组 - Send data from .txt file to JavaScript array 我可以使用 javascript 将 HTML 表单数据(包括单选按钮)保存为本地 txt 文件吗? - Can I save a HTML form data (including radio button) as a local txt file using javascript? 如何从并行 JavaScript arrays 创建.txt 文件? - How can I create .txt file from parallel JavaScript arrays? 如何读取 txt 文件并将其保存在 html 中 javascript 中的数组中 - How to read txt file and save it in array in javascript in html 如何将tExt块附加到canvas数据数组中并将其另存为javascript中的png文件? - How can I append a tExt chunk into a canvas data array and save it as a png file in javascript? 如何将所有 javascript output 保存到 txt 文件中以备后用? - How can i save all javascript output in to a txt file for later use? 如何使用javascript将表单数据附加并保存到txt文件 - How to append and save form data to txt file using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM