开发者

Modifying values in an array

I have an array with string values. I want to add additional text before or after each value in the array. How can I do this?

From what I have seen开发者_如何学运维 I am guessing it will be something like:

$.each(array, function() {
   // something here
});

Everything I have tried doesn't seem to be working.


I think you can use plain JavaScript, that runs a little bit faster.

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


You're on the right track. Try:

$.each(array, function(i, v){
   array[i] = array[i] + 'hello';
});

You could also use map:

var newArray = $.map(array, function(v, i) {
   return v + 'hello';
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜