如何拆分包含数组的数组?

时间:2018-02-20 18:36:45

标签: javascript arrays algorithm arraylist

你好我的JS朋友们,

我让用户导入csv文件(excel表)并将其转换为 数组。在这种情况下,它有472行和87列。

所以我的数组看起来像这样: enter image description here

并且所有内容都用逗号分隔,就像通常的数组一样。

问题是我需要在数组中分离数组,当我这样做时,我得到一个长度为900万的数组,我认为这是错误的

        vm.allTextLines = files.split(/\r\n|\n/);

        var headers = vm.allTextLines[0].split(',');

        vm.columnCount = headers.length;
        vm.rowCount = vm.allTextLines.length - 1;

        for (var i = 0; i < vm.allTextLines.length; i++) {
            // split content based on comma
            var data = vm.allTextLines[i].split(',');
            if (data.length == headers.length) {
                var tarr = [];
                for (var j = 0; j < headers.length; j++) {
                    tarr.push(data[j]);
                }
                vm.lines.push(tarr);
            }
        }

            //this is where i split the array that contains the csv
            //data and put it into its own array I believe this is 
            //where the issue is.
            for(var i=1;i<vm.allTextLines.length; i++){

            vm.uniqueAll.push(vm.allTextLines[i].split(','));

            for(var j=0; j < vm.uniqueAll.length; j++){

                for(var r =0; r < vm.uniqueAll[j].length; r++){

                    vm.arrayOfValuesOfFile.push(vm.uniqueAll[j][r]);
                }
            }

        }

如果你能帮助我纠正这个问题,我会很感激。

先谢谢你们!

2 个答案:

答案 0 :(得分:1)

我同意你关于错误的地方,因为你似乎以错误的方式嵌套循环。下面的片段,你可以检查我的意思。 即:

&#13;
&#13;
let vm = {
  allTextLines:['h1,h2,h3','row1val1,row1val2,row1val3', 'row2val1,row2val2,row2val3'],
  uniqueAll: [],
  arrayOfValuesOfFile:[]
}
// Here you should not nest the loop 
for(var i=1;i<vm.allTextLines.length; i++){
  vm.uniqueAll.push(vm.allTextLines[i].split(','));
}
for(var j=0; j < vm.uniqueAll.length; j++){
  for(var r =0; r < vm.uniqueAll[j].length; r++){
    vm.arrayOfValuesOfFile.push(vm.uniqueAll[j][r]);
  }
}

console.log('allTextLines', vm.allTextLines);
console.log('uniqueAll', vm.uniqueAll);
console.log('arrayOfValuesOfFile', vm.arrayOfValuesOfFile);
&#13;
&#13;
&#13;

当然,您可以轻松优化算法:

&#13;
&#13;
let vm = {
  allTextLines:['h1,h2,h3','row1val1,row1val2,row1val3', 'row2val1,row2val2,row2val3'],
  uniqueAll: [],
  arrayOfValuesOfFile:[]
}

for(var i=1;i<vm.allTextLines.length; i++){
  let currentLinesValue = vm.allTextLines[i].split(',');
  vm.uniqueAll.push(currentLinesValue);
    for(var r =0; r < currentLinesValue.length; r++){
      vm.arrayOfValuesOfFile.push(currentLinesValue[r]);
    
  }
}
console.log('allTextLines', vm.allTextLines);
console.log('uniqueAll', vm.uniqueAll);
console.log('arrayOfValuesOfFile', vm.arrayOfValuesOfFile);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

首先,您应该将二维数组转换为一维数组。

var allTogether = []; // Array with all your CSV (no matter from which file it came from)
for (var i = 0; vm.allTextLines.length; i++) {
    allTogether.push(vm.allTextLines[i]); // Gets the CSV line an adds to a one-dimension array
}

// Now you can iterate over the one-dimension array
for (var i = 0; allTogether.length; i++) {
    var csvFields = allTogether[i].split(',');
    // Here goes your code that works with the CSV fields.
}