I tried to get some Data from a csv file with the following code: (The csv file are several lines which are different rows from a table)
var newArray = []
function init() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var lines = this.responseText.split("\n");
newArray.push(lines)
}
}
xhttp.open("GET", "CMDBsox.csv", true);
xhttp.send();
}
window.onload = init;
console.log(array)
The csv file looks like this:
(I just noticed that the first element of each line is not in quotes " )
First line: Lorem1, "Lorem2", "Lorem3", "Lorem4",...
Second line: Ipsum1, "Ipsum2", "Ipsum3", "Ipsum4",...
and so on
I pushed the 'lines' array into a new array called newArray
.
Now I can see the array but all rows are in another array like:
0:(21) [...]
0:["Sample1", "Sample2", "Sample3,...]
1:["Example1", "Example2", Example3",...]
2:["Test1", "Test2", "Test3",...]
and so on...(21 times)
I can access the different arrays(rows) with:
"newArray[0][0]", "newArray[0][1]", "newArray[0][2]",...
but now I have trhee problems:
I can acces them via console but not via code... when I write
newArray[0][1]
I get the error**"TypeError: array[0] is undefined"**.
But in the console I can see the whole array?How can I create new arrays inside the array. For example, now I have a big string inside
newArray[0][0]
but I need the single elements in an arrayI currently access every "line" via
newArray[0][i]
but I want to access them withnewArray[i]..
how can I move them into the "upper" array so I don't have that useless first array
Thank You :)