I have an array which contains the contents as follows:
[\"ZS125-48ATab\", \"STR125YBTab\", \"KS125-24Tab\", \"ZS125-50Tab\", \"DFE125-8ATab\", \"ZS125-30Tab
Iterate over the array and just add #
var arr = [your array];
for (var i=arr.length; i--;) {
arr[i] = '#' + arr[i];
}
FIDDLE
In newer browsers you could do
arr = arr.map(function(e) {return '#' + e});
Example for ES6
var arr = ['first', 'second', 'third'];
arr = arr.map(i => '#' + i);
Result:
console.log(arr); // ["#first", "#second", "#third"]
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...
array = ('#' + array.join('#')).split(/(?=#)/);
Indeed, I rather expected this scenario : "#a#b#c" -> ["", "#a", "#b", "#c"]
.
Anyway, I prefer the second method since match
returns null
in case of failure.
Simple & sweet in ES6 as,
array.map((line) => `#${line}`);
for(var i=0;i<array.length;i++){
array[i]="#"+array[i];
}
Use the forEach
method(reference)
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;
});