0

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:

  1. 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?

  2. 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 array

  3. I currently access every "line" via newArray[0][i] but I want to access them with newArray[i].. how can I move them into the "upper" array so I don't have that useless first array

Thank You :)

House97_
  • 373
  • 1
  • 3
  • 10

1 Answers1

0

I do not understand why you are diverting your question on the problem of reading a file while it is only a problem on the copy of a table in another.
Asking clear questions is essential if you expect good answers.

so push a array inside a array is bad idea, use Object.assign

var arr1 = [ 1,7 ,3 ,4 ,5];

var arr2 = [[]];


Object.assign (arr2[0], arr1)

console.log ('arr1 ...: ', JSON.stringify(arr1));
console.log ('arr2[0] : ', JSON.stringify(arr2[0]));
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40