Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
23
rated 0 times [  28] [ 5]  / answers: 1 / hits: 28717  / 10 Years ago, tue, september 30, 2014, 12:00:00

I have an array of values to which I want to add some prefix:


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

Adding the prefix images/ should result in this:


newArr = ["images/1.jpg", "images/2.jpg", "images/some.jpg"];

More From » javascript

 Answers
234

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)

[#69290] Friday, September 26, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
loganl

Total Points: 424
Total Questions: 86
Total Answers: 112

Location: Zimbabwe
Member since Thu, Jul 21, 2022
2 Years ago
;