【问题标题】:Add new string between every current string in array [duplicate]在数组中的每个当前字符串之间添加新字符串[重复]
【发布时间】:2020-05-16 04:30:37
【问题描述】:

我有一系列主题

["dogs", "cats", "fish", "morestuff"]

我稍后会格式化这个数组,以用作一系列在match() 中匹配的标识。考虑到这个列表会增长,我实际上是在使用这个字符串数组来转换为 OR 运算符的大型条件语句。这将在eval(x.join('')) 内执行。所以我想要一种在数组中每个现有字符串之间生成|| 运算符的方法。

这是想要的输出

["dogs", "||", "cats", "||", "fish", "||", "morestuff"]

如果这很复杂,请随意分享更好的方法。谢谢

【问题讨论】:

标签: javascript node.js


【解决方案1】:

var array = ["dogs", "cats", "fish", "morestuff"],
    result = array.reduce((r, a) => r.concat(a, "||"), []);
    
console.log(result);

【讨论】:

  • 它会产生这样的结果 [ "dogs", "||", "cats", "||", "fish", "||", "morestuff", "||" ]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-02
  • 2021-07-26
  • 2012-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多