1

I am receiving in my App an Array of text from a backend I have no control over

Array

this.Array = ["/tms/open-attachment/121", "/tms/open-attachment/122"]

I am trying to add "some text added here" to the beginning

this.Array =  ["some text added here/tms/open-attachment/121", "some text added here/tms/open-attachment/122"]

Using

for (let m = 0; m <this.Array.length; m++) { 

}

I have been able to add to the end of the text but not the front.

Is there an easy way to add some custom text to the beginning of the string?

2
  • Array is not a reseved word in JS ? Commented Nov 19, 2020 at 1:20
  • Why can you add to the end but not the beginning? It's just new + a versus a + new. Commented Nov 19, 2020 at 1:53

2 Answers 2

4

Use .map and concatenate what you need:

const arr = ["/tms/open-attachment/121", "/tms/open-attachment/122"];
const result = arr.map(str => 'some text added here' + str);
console.log(result);

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

Comments

1

If you want to use a traditional for loop:

for (let i = 0; i < array.length; i++) { 
    array[i] = "some text" + array[i];
}

Or, as said CertainPerformance, you can use a Array.prototype.map():

array = array.map(str => "some text" + str)

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.