Javascript 3D array and .split () method
function parser(data){
var saveSections = data.split("\r");
var parsed = new Array();
var tempCatch = "";
var CatchTwo = [];
//saveSections[0] = saveSections[0].split(",");
for (var i = 0; i < saveSections.length; i++){
saveSections[i] = saveSections[i].split(",");
for (var j = 0; j < saveSections.length; j++){
tempCatch = saveSections[0][0].split(":");
//saveSections[0][0] = tempCatch;
}
}
return tempCatch;
}
Okay, there is a problem. This feature works without issue until I uncomment
//saveSections[0][0] = tempCatch;
...
When I do this, debbuger throws:
Uncaught TypeError: saveSections[0][0].split is not a function
This points to this line:
tempCatch = saveSections[0][0].split(":");
source to share
From the example data you provided, the problem is that you are always assigning to the tempCatch
first element in the 2D array ( saveSections[0][0]
), and on the second iteration, the function split()
fails because an array, not a string.
This code should iterate over all elements:
function parser(data){
var saveSections = data.split("\r");
var parsed = [];
var tempCatch = "";
var CatchTwo = [];
for (var i = 0; i < saveSections.length; i++){
saveSections[i] = saveSections[i].split(",");
for (var j = 0; j < saveSections[i].length; j++){
tempCatch = saveSections[i][j].split(":");
saveSections[i][j] = tempCatch;
}
}
return saveSections;
}
I would suggest that you need to return saveSections
instead tempCatch
, but this is a bit unclear from your implementation.
source to share