Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding text to beginning of each array element

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.

like image 511
user3004208 Avatar asked Dec 10 '13 15:12

user3004208


People also ask

How do you add a value to the start of an array?

The unshift() method adds new elements to the beginning of an array. The unshift() method overwrites the original array.

How do you add items to each element in an array?

By using ArrayList as intermediate storage: Create an ArrayList with the original array, using asList() method. Simply add the required element in the list using add() method. Convert the list to an array using toArray() method.

Which function is used to adds an element to the beginning of an array?

If you need to add an element to the beginning of your array, try unshift(). And you can add arrays together using concat().


2 Answers

Example for ES6

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

Result:

console.log(arr); // ["#first", "#second", "#third"] 
like image 160
nash.pro Avatar answered Sep 29 '22 21:09

nash.pro


for(var i=0;i<array.length;i++){     array[i]="#"+array[i]; } 
like image 27
Fibonacci Avatar answered Sep 29 '22 21:09

Fibonacci