How to add prefix to array values?

You can simply do this with a simple loop:

var arr = ["1.jpg","2.jpg","some.jpg"],
    newArr = [];

for(var i = 0; i<arr.length; i++){
    newArr[i] = 'images/' + arr[i];
}

Array.prototype.map is a great tool for this kind of things:

arr.map(function(el) { 
  return 'images/' + el; 
})

In ES2015+:

arr.map(el => 'images/' + el)

Use Array.prototype.map():

const newArr = arr.map(i => 'images/' + i)

Same thing but without using ES6 syntax:

var arr = arr.map(function (i){
    return 'images/' + i;
})

For browser compatibility and without loop:

var pre = 'images/';
var arr = ['1.jpg', '2.jpg', 'some.jpg'];
var newArr = (pre + arr.join(';' + pre)).split(';');

Tags:

Javascript