2016-08-19 112 views
1

我有一個JavaScript數組是這樣的:字符串數組的JavaScript

["444c-0300-0b29-1817", "444c-0300-0b29-0715,444c-0300-0b29-0720"] 

我要讓這樣的:

["444c-0300-0b29-1817", "444c-0300-0b29-0715", "444c-0300-0b29-0720"] 

我需要一個最好的做法.. 感謝您的幫助。

+0

也可以使用'變種arrayVal = [ 「444C-0300-0b29-1817」, 「444C-0300-0b29-0715,444c-0300-0b29-0720」]; arrayVal = arrayVal.join()。split(「,」);' –

回答

1

你可以使用Array#reduceArray#concat

var data = ["444c-0300-0b29-1817", "444c-0300-0b29-0715,444c-0300-0b29-0720"], 
 
    single = data.reduce(function (r, a) { 
 
     return r.concat(a.split(',')); 
 
    }, []); 
 

 
console.log(single);

ES6

var data = ["444c-0300-0b29-1817", "444c-0300-0b29-0715,444c-0300-0b29-0720"], 
 
    single = data.reduce((r, a) => r.concat(a.split(',')), []); 
 

 
console.log(single);

+0

非常感謝你,你救了我.. – regex

0

另外一個做這份工作的方式;

var arr = ["444c-0300-0b29-1817", "444c-0300-0b29-0715,444c-0300-0b29-0720"], 
 
    brr = [].concat(...arr.map(s => s.split(","))); 
 
console.log(brr);

+0

這個解決方案也不錯。謝謝 – regex