gpt4 book ai didi

javascript - 从文本中获取数组

转载 作者:行者123 更新时间:2023-11-28 00:40:23 25 4
gpt4 key购买 nike

我一直在尝试这段代码http://mounirmesselmeni.github.io/2012/11/20/javascript-csv/从文本文件中获取数据。 (此处的工作演示:http://mounirmesselmeni.github.io/html-fileapi/)。

它非常适合读取文件,但我对如何将数据放入数组感到困惑。似乎它正在将所有内容读入“lines”数组,但我不知道如何使用它。

我尝试像这样修改它:

function processData(csv) {
var allTextLines = csv.split(/\r\n|\n/);
var lines = [];
var myArray = [];
while (allTextLines.length) {
lines.push(allTextLines.shift().split(','));
myArray.push(allTextLines.shift().split(',')); //put data into myArray
}

function myFunction() { //display myArray in "demo"
var index;
for (index = 0; index < myArray.length; index++) {
text += myArray[index];
}
document.getElementById("demo").innerHTML = text;
}

但这没有用。我知道我在这里错过了一些简单的东西,但这让我很难过。

最佳答案

当前您修改数组两次:

lines.push(allTextLines.shift().split(','));     // shift modifies the array
myArray.push(allTextLines.shift().split(',')); //gets the shifted array

您可能想尝试将其放入临时变量中:

var line = allTextLines.shift().split(',');
lines.push(line);
myArray.push(line);

关于javascript - 从文本中获取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28011302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com