0

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

3 Answers 3

3

Use join with the same delimiters.

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

Sign up to request clarification or add additional context in comments.

Comments

2

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>');

2 Comments

What is 0, 4 there in JSON.Stringify()
1
var arrFinal = [];

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


console.log(arrFinal)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.