简体   繁体   中英

split array with delimiter into string

I have an array like below

["abc", "stued(!)rain(!)shane", "cricket"]

How can i convert this to string by spliting on (#) like below:

"abc"
"stued"
"rain"
"shane"
"cricket"

Below is the code i have tried

var arr = ["abc", "stued(!)rain(!)shane", "cricket"];
console.log(arr.join('').split("(!)"));

I am getting abcstued,rain,shanecricket which is not the desired output

Use join with the same delimiters.

 var arr = ["abc", "stued(!)rain(!)shane", "cricket"]; alert(arr.join('(!)').split("(!)")); 

This is missing, a solution with Array#reduce:

 var arr = ["abc", "stued(!)rain(!)shane", "cricket"], result = arr.reduce(function (r, a) { return r.concat(a.split('(!)')); }, []); document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>'); 

var arrFinal = [];

arr.forEach(function(val, key) {
        arrFinal = arrFinal.concat(val.split('(!)'))
    })


console.log(arrFinal)

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