Javascript快捷方式,将字符串追加到数组的所有值? [英] Javascript shortcut, to append string into all the values of array?

查看:87
本文介绍了Javascript快捷方式,将字符串追加到数组的所有值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组= [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.

我的预期输出是类似的

["Hello_a", "Hello_b", "Hello_c"]

javascript中是否有任何快捷方式可以执行此操作,而不使用任何循环。

Is there any shortcut in javascript to perform this operation, without using any loop .

任何帮助都是赞赏!

谢谢

推荐答案

尝试使用 Array.prototype.map()在此上下文中,

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.

这篇关于Javascript快捷方式,将字符串追加到数组的所有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆