【问题标题】:Retaining value commas but removing separator commas in arrays保留值逗号但删除数组中的分隔符逗号
【发布时间】:2017-02-27 14:09:07
【问题描述】:

希望我的措辞正确。

我有一个数组:

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

我需要做些什么才能做到这一点?

【问题讨论】:

  • 使用.join() method -> thisArray.join('&lt;br /&gt;')
  • @hindmost 替换数组?不会工作。

标签: javascript jquery arrays string


【解决方案1】:

您可以使用任何东西作为分隔符加入数组:

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

【讨论】:

  • 我很高兴,但很确定@Josh Crozier 首先回答(评论):)
猜你喜欢
  • 2019-02-12
  • 1970-01-01
  • 2017-09-13
  • 2013-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-10
相关资源
最近更新 更多