简体   繁体   中英

Creating Array of Array from string

Hey guys am a learner and I have a string which I want to convert to an array

var myString = 'one,two\nthree,four\nfive\nsix,seven'

I want to convert this to an array of array like below

var newArray = [['one','two'],['three','four'],['five'],['six','seven']]

In my newArray I want to convert the line break in myString to an array and also split the words and put them as individual elements in an array

I hope I see help

You can split on a newline, and then use map to split all the parts on a comma.

 var myString = 'one,two\nthree,four\nfive\nsix,seven'; var newArray = myString.split("\n").map(s => s.split(",")); console.log(newArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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