繁体   English   中英

读取文本文件并将其保存到列表中 (JavaScript)

[英]Read a text file and save it into a list (JavaScript)

我在文本文件中有一个名词列表。 我想用 JavaScript 阅读这篇文章,将其保存在一个变量中,并将其拆分为字符串列表(对应于每个名词)。 我需要这些名词来为实验创建刺激。 这是我尝试过的,但似乎不起作用:

function readNamesfromFileandCreateList (audiostim_name){
    //read from file a list of words, save it in a list and and shuffle it (twice for good measure)
    var openedText = await fetch(audiostim_name);
    var words = openedText.split(/\r\n|\n/);
    jsPsych.randomization.shuffle(words); jsPsych.randomization.shuffle(words); 
    return words
}

我得到的错误是.split()不是 function 并且我的文本文件无法访问或加载。 我正在使用网络服务器。

Fetch 返回一个响应 - 您需要调用response.text()来获取可以拆分的字符串

function readNamesfromFileandCreateList (audiostim_name){
    //read from file a list of words, save it in a list and and shuffle it (twice for good measure)
    var response = await fetch(audiostim_name);
    var openedText = await response.text(); // <-- changed
    var words = openedText.split(/\r\n|\n/);
    jsPsych.randomization.shuffle(words); jsPsych.randomization.shuffle(words); 
    return words
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM