1

How can I append/concatenate some string before each array element using join()? For example the following code has an output like

One_Two_Three_Four

but what I want is adding the string Step- to the beginning of all items on join which can have an output like:

Step-One_Step-Two_Step-Three_Step-Four

var steps = ["One", "Two", "Three", "Four"];
var stpstr = steps.join("_");

console.log(stpstr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

2
  • join with _Step- and also add a _Step- in the beginning ? Commented Dec 12, 2020 at 7:31
  • Hi the join with _step- is adding the string _step- to the end of each element but as I asked I want to add to the beginning of each phrase Commented Dec 12, 2020 at 7:38

1 Answer 1

4

This should work:

steps.map(e => "Step-" + e).join("_")

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.