I have an array = ["a", "b", "c"];
What I want is i have an string let us say "Hello", that I want to append to each and every value of this array.
My expected output is something like
["Hello_a", "Hello_b", "Hello_c"]
Is there any shortcut in javascript to perform this operation, without using any loop .
Any help is appreciated !
Thanks
Try to use Array.prototype.map()
at this context,
var yourArray = ["a", "b", "c"];
var transformed = yourArray.map(function(item){
return "Hello_" + item;
});
console.log(transformed); // ["Hello_a", "Hello_b", "Hello_c"]
Also you can use fat Arrow functions if users of you are updated with latest browsers.
var yourArray = ["a", "b", "c"];
var transformed = yourArray.map(item => "Hello_" + item);
console.log(transformed); // ["Hello_a", "Hello_b", "Hello_c"]
As a side note, you have to sure about arrow functions that it will forcibly use lexical this
inside of it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.