How convert string to array in JS, spilt not work

Viewed 41

There has a problem. I read data from a CSV file then store the innerHTML to a var(named content), it can read by the browser and the type is a string. When I going to convert it to the array, it not work. I tried split() but it does not work correctly for me.

 function readAsText() {
            var file = document.getElementById("file").files[0];
            var reader = new FileReader();

            reader.readAsText(file);
            reader.onload = function (f) {
                var result = document.getElementById("result");
                result.innerHTML = this.result;
                content = result.innerHTML
                arr1 = content.split(" /n")
                arr2 = content.split(" ")
            }
        }

As the picture, split() can split the string, but the result in a single item

enter image description here

2 Answers

It should be a backslash, not a forward slash.

arr1 = content.split("\n")

You should use backslash at your code:

content.split("\n")

let content = `ID1
R34234
B13242
C24234
`
let arr1= content.split("\n")
console.log(arr1)
console.log(arr1.length)

Related