How can I convert a comma-separated string to an array?

后端 未结 15 1603
温柔的废话
温柔的废话 2020-11-22 02:37

I have a comma-separated string that I want to convert into an array, so I can loop through it.

Is there anything built-in to do this?

For example, I have this

15条回答
  •  甜味超标
    2020-11-22 03:20

    I had a similar issue, but more complex as I needed to transform a CSV file into an array of arrays (each line is one array element that inside has an array of items split by comma).

    The easiest solution (and more secure I bet) was to use PapaParse which has a "no-header" option that transform the CSV file into an array of arrays, plus, it automatically detected the "," as my delimiter.

    Plus, it is registered in Bower, so I only had to:

    bower install papa-parse --save
    

    And then use it in my code as follows:

    var arrayOfArrays = Papa.parse(csvStringWithEnters), {header:false}).data;
    

    I really liked it.

提交回复
热议问题