简体   繁体   English

如何在JS中将字符串转换为数组,溢出不起作用

[英]How convert string to array in JS, spilt not work

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.我从 CSV 文件中读取数据,然后将 innerHTML 存储到 var(命名内容)中,它可以被浏览器读取,类型是字符串。 When I going to convert it to the array, it not work.当我要将它转换为数组时,它不起作用。 I tried split() but it does not work correctly for me.我试过 split() 但它对我来说不能正常工作。

 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如图, split() 可以拆分字符串,但结果为单个项目

在此处输入图像描述

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM