Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript嵌套for循环未按预期返回数组_Javascript_Loops_For Loop_Nested - Fatal编程技术网

Javascript嵌套for循环未按预期返回数组

Javascript嵌套for循环未按预期返回数组,javascript,loops,for-loop,nested,Javascript,Loops,For Loop,Nested,伙计们,我想不起来了。我有一个通过AJAX读取的CSV文件,我正在根据返回的内容创建一个数组。我的CSV文件如下所示: ID,name,address 0,john,123 fake st 1,bruce,123 fake st 2,john,124 fake st 3,fred,125 fake st 4,barry,126 fake st for (i = 1; i < cols.length; i++) { line = [ ]; // <-------- Thi

伙计们,我想不起来了。我有一个通过AJAX读取的CSV文件,我正在根据返回的内容创建一个数组。我的CSV文件如下所示:

ID,name,address
0,john,123 fake st
1,bruce,123 fake st
2,john,124 fake st
3,fred,125 fake st
4,barry,126 fake st
for (i = 1; i < cols.length; i++) {
    line   = [ ]; // <-------- This is the important part!
    curRow = cols[i].split(',');
    for (j = 0; j < curRow.length; j++) {
        line[ln1[j]] = curRow[j];
    }
    table[curRow[0]] = line;
}
我通过ajax函数调用它:

if (window.XMLHttpRequest) {
    var ajax = new XMLHttpRequest();
} else var ajax = new ActiveXObject("Microsoft.XMLHTTP");

function include(src) {
    ajax.open('GET', src, false);
    ajax.send(null);
    return ajax.responseText;
}
然后像这样循环:

var bsf = include('csv.csv');
// construct an array from the first line of the file
// and use that array to name the keys in all further arrays
var cols = bsf.split('\r\n');
var ln1 = cols[0].split(',');
// pull out each line from bsf and turn it into an array
var line = [];
var table = {};
var curRow = [];

for (i = 1; i < cols.length; i++) { // i = 1 so we can skip the 'title' csv line
    curRow = cols[i].split(',');

    for (j = 0; j < curRow.length; j++) {
        line[ln1[j]] = curRow[j];
    }

    table[curRow[0]] = line;
}
console.dir(table);
我明白了


有什么想法吗?我感觉我在整个循环中都正确地分配了它们,但在最后一次运行时,我错误地分配了它们,并覆盖了正确的部分。

您的问题是,您只有一个
数组可以反复填充:

for (j = 0; j < curRow.length; j++) {
    line[ln1[j]] = curRow[j];
}
结果是
表中的每个条目都将是
cols
中的最后一行

您只需将不同的数组放入
表中
,如下所示:

ID,name,address
0,john,123 fake st
1,bruce,123 fake st
2,john,124 fake st
3,fred,125 fake st
4,barry,126 fake st
for (i = 1; i < cols.length; i++) {
    line   = [ ]; // <-------- This is the important part!
    curRow = cols[i].split(',');
    for (j = 0; j < curRow.length; j++) {
        line[ln1[j]] = curRow[j];
    }
    table[curRow[0]] = line;
}
for(i=1;iline=[];//此csv是动态生成的吗?如果是,服务器应将其作为json字符串发送过来,这样就省去了对csv数据进行客户端解析的所有麻烦。run
console.log(json.stringify(table));
(facepalm)@Black\u Stormy您不应该使用
数组
,因为您真正想要的是
对象
(散列).Ah我需要重新初始化数组以覆盖它的内容。我不知道这一点。谢谢!@Black\u Stormy:你不需要重新初始化数组,而是要创建一个全新的数组,否则你会得到
a=[…];table[0]=a;table[1]=a;…
for (i = 1; i < cols.length; i++) {
    line   = [ ]; // <-------- This is the important part!
    curRow = cols[i].split(',');
    for (j = 0; j < curRow.length; j++) {
        line[ln1[j]] = curRow[j];
    }
    table[curRow[0]] = line;
}