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';
});
精彩评论