将带分隔符的数组拆分为字符串

时间:2016-02-04 18:05:08

标签: javascript

我有array,如下所示

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

如何通过分割string如下所示将其转换为(#)

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

以下是我试过的代码

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

我收到的abcstued,rain,shanecricket不是所需的output

3 个答案:

答案 0 :(得分:3)

join与相同的分隔符一起使用。



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




答案 1 :(得分:2)

缺少这个,使用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>');
&#13;
&#13;
&#13;

答案 2 :(得分:1)

$this->response->redirect($this->url->link('checkout/checkout', '', 'SSL'));