40

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 Answers 7

66

Example for ES6

var arr = ['first', 'second', 'third'];    
arr = arr.map(i => '#' + i);

Result:

console.log(arr); // ["#first", "#second", "#third"]
Sign up to request clarification or add additional context in comments.

1 Comment

Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem.
30
for(var i=0;i<array.length;i++){
    array[i]="#"+array[i];
}

1 Comment

` array.forEach(item => { item = "#" + item }) `
21

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});

6 Comments

I can understand when newcomers do that, but I don't think high-rep users should answer "questions" like this. A closevote (+ a friendly "what have you tried" comment) would be a way more helpful.
@thg435 - seems like a legitimate question to me, even if it's not something that's really hard to do, or figure out with a little trial and error.
"Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results."
@thg435 - That is true, and if you feel it should be closed, vote for that, that's up to you.
Of course I did, and downvoted yours. "Gimme some codez" questions as well as "gimme some repz" answers don't belong here.
|
12

Simple & sweet in ES6 as,

array.map((line) => `#${line}`);

Comments

2

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.

1 Comment

@acostache Trickier but probably more expensive :-)
1

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;
});

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.