2017-02-27 97 views
0

希望我正確寫法。保留值逗號,但刪除數組中的分隔符逗號

我有一個數組:

var thisArray = ["This","That","This and That","This, That or Something Else"]; 

而我想做的就是這個轉換成字符串,並將其轉換時,取代從休息標籤的所有逗號分隔符,但保留每一個值內的逗號。

現在,我的代碼塊:

var thisArray = ["This","That","This and That","This, That or Something Else"]; 
var thisString = thisArray.toString(); 
var thisResult = thisString.replace(",","<br/>"); 
return thisResult; 

不過,當然,這樣一來,將網着我的實際結果是:

This 
That 
This and That 
This 
That or Something Else 

當我想要得到的結果是

This 
That 
This and That 
This, That or Something Else 

爲了做到這一點,我需要做些什麼?

+9

使用['。加入()'方法](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join) - >'thisArray.join('
')' –

+0

@最後一個數組替換?不去上班。 – epascarello

回答

0

可以使用任何作爲隔板加入一個陣列:

return thisArray.join('<br />') 
+0

謝謝你這麼快速的回答。 – Murphy1976

+1

我的榮幸,但很確定@Josh Crozier回答(評論)第一:) –

+0

我不能標記評論作爲正確的答案。 – Murphy1976