【问题标题】:What is the simple way to concatenate all strings on a list with a string in NodeJs? [duplicate]将列表中的所有字符串与 NodeJs 中的字符串连接起来的简单方法是什么? [复制]
【发布时间】:2020-10-26 10:36:14
【问题描述】:

我知道怎么做,我只是想要一个更简单的方法来实现它。

例如,如果我有以下列表:

["This", "Is", "An", "Example"]

还有以下字符串:

const str = "-abc"

我想要的结果是:

["This-abc", "Is-abc", "An-abc", "Example-abc"]

【问题讨论】:

  • 您将遍历数组,并且有很多函数可以做到这一点。 .map() 可能有用

标签: javascript node.js string list concatenation


【解决方案1】:

您可以使用map,它将对数组中的每个项目执行一个函数,并创建一个新的:

const arr = ["This", "Is", "An", "Example"];
const str = "-abc";

const res = arr.map(x => x + str);

console.log(res);

【讨论】:

    【解决方案2】:

    您可以使用地图功能来实现这一点。

    const arr = ["This", "Is", "An", "Example"];
    const strToConcat = "-abc";
    const formattedArr = arr.map(str => str+strToConcat);
    
    console.log(formattedArr);
    

    【讨论】:

      猜你喜欢
      • 2013-10-15
      • 1970-01-01
      • 2019-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-11
      • 1970-01-01
      相关资源
      最近更新 更多