Search code examples
javascriptnode.jsstringlistconcatenation

What is the simple way to concatenate all strings on a list with a string in NodeJs?


I know how to do this, I just want a simpler way to implement this.

For example if I have the following list:

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

And the following string:

const str = "-abc"

The result I want is:

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

Solution

  • You can use the map function to achieve this.

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