Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
496 views
in Technique[技术] by (71.8m points)

javascript - How to add prefix to array values?

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"];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...