I have an array which contains the contents as follows:
我有一个数组,其中包含如下内容:
["ZS125-48ATab", "STR125YBTab", "KS125-24Tab", "ZS125-50Tab", "DFE125-8ATab", "ZS125-30Tab", "HT125-8Tab", "HT125-4FTab", "STR50Tab"]
Is it possible to append a #
symbol to the front of each element in the array.
是否可以将#符号附加到数组中每个元素的前面。
Thanks.
谢谢。
7 个解决方案
#1
13
for(var i=0;i<array.length;i++){
array[i]="#"+array[i];
}
#2
16
Iterate over the array and just add #
遍历数组并添加#
var arr = [your array];
for (var i=arr.length; i--;) {
arr[i] = '#' + arr[i];
}
小提琴
In newer browsers you could do
在更新的浏览器中,你可以这样做
arr = arr.map(function(e) {return '#' + e});
#3
10
Example for ES6
例子ES6
var arr = ['first', 'second', 'third'];
arr = arr.map(i => '#' + i);
Result:
结果:
console.log(arr); // ["#first", "#second", "#third"]
#4
3
You can do it like this :
你可以这样做:
array = ('#' + array.join('#')).match(/#[^#]*/g) || []; // null || []
The following trick works as well, but I wonder why split
ignores the first sharp...
下面的技巧也同样有效,但是我想知道为什么split忽略了第一个sharp……
array = ('#' + array.join('#')).split(/(?=#)/);
Indeed, I rather expected this scenario : "#a#b#c" -> ["", "#a", "#b", "#c"]
.
事实上,我预计这一场景:“# # # c b”- >[”“、”#“b”#”、“# c”)。
Anyway, I prefer the second method since match
returns null
in case of failure.
无论如何,我更喜欢第二个方法,因为如果失败,match将返回null。
#5
1
Use the forEach
method(reference)
使用forEach方法(参考)
var array = ["ZS125-48ATab", "STR125YBTab", "KS125-24Tab", "ZS125-50Tab", "DFE125-8ATab", "ZS125-30Tab", "HT125-8Tab", "HT125-4FTab", "STR50Tab"];
array.forEach(function(element, index) {
array[index] = '#' + element;
});
#6
1
The following code would do the job:
下面的代码将完成这项工作:
var t = ["ZS125-48ATab", "STR125YBTab", "KS125-24Tab", "ZS125-50Tab", "DFE125-8ATab", "ZS125-30Tab", "HT125-8Tab", "HT125-4FTab", "STR50Tab"];
for(var i=0;i<t.length;i++){
t[i] = "#"+t[i];
}
See demo here
看到演示
#7
0
Simple & sweet in ES6 as,
简单甜蜜的ES6 as,
array.map((line) => `#${line}`);
#1
13
for(var i=0;i<array.length;i++){
array[i]="#"+array[i];
}
#2
16
Iterate over the array and just add #
遍历数组并添加#
var arr = [your array];
for (var i=arr.length; i--;) {
arr[i] = '#' + arr[i];
}
小提琴
In newer browsers you could do
在更新的浏览器中,你可以这样做
arr = arr.map(function(e) {return '#' + e});
#3
10
Example for ES6
例子ES6
var arr = ['first', 'second', 'third'];
arr = arr.map(i => '#' + i);
Result:
结果:
console.log(arr); // ["#first", "#second", "#third"]
#4
3
You can do it like this :
你可以这样做:
array = ('#' + array.join('#')).match(/#[^#]*/g) || []; // null || []
The following trick works as well, but I wonder why split
ignores the first sharp...
下面的技巧也同样有效,但是我想知道为什么split忽略了第一个sharp……
array = ('#' + array.join('#')).split(/(?=#)/);
Indeed, I rather expected this scenario : "#a#b#c" -> ["", "#a", "#b", "#c"]
.
事实上,我预计这一场景:“# # # c b”- >[”“、”#“b”#”、“# c”)。
Anyway, I prefer the second method since match
returns null
in case of failure.
无论如何,我更喜欢第二个方法,因为如果失败,match将返回null。
#5
1
Use the forEach
method(reference)
使用forEach方法(参考)
var array = ["ZS125-48ATab", "STR125YBTab", "KS125-24Tab", "ZS125-50Tab", "DFE125-8ATab", "ZS125-30Tab", "HT125-8Tab", "HT125-4FTab", "STR50Tab"];
array.forEach(function(element, index) {
array[index] = '#' + element;
});
#6
1
The following code would do the job:
下面的代码将完成这项工作:
var t = ["ZS125-48ATab", "STR125YBTab", "KS125-24Tab", "ZS125-50Tab", "DFE125-8ATab", "ZS125-30Tab", "HT125-8Tab", "HT125-4FTab", "STR50Tab"];
for(var i=0;i<t.length;i++){
t[i] = "#"+t[i];
}
See demo here
看到演示
#7
0
Simple & sweet in ES6 as,
简单甜蜜的ES6 as,
array.map((line) => `#${line}`);