How to append a dot at the beginning of a string in array-Javascript - Stack Overflow

I have an array:arr= ["abd","test","testss"];I want to add dot in front o

I have an array:

arr= ["abd","test","testss"];

I want to add dot in front of all the strings like:

arr= [".abd",".test",".testss"];

this failed:

arr.forEach(function(e){
  e.append(".")
})

how can I do this?

I have an array:

arr= ["abd","test","testss"];

I want to add dot in front of all the strings like:

arr= [".abd",".test",".testss"];

this failed:

arr.forEach(function(e){
  e.append(".")
})

how can I do this?

Share Improve this question asked Oct 10, 2018 at 23:09 user1234user1234 3,1596 gold badges57 silver badges117 bronze badges 2
  • Have a look at developer.mozilla/en-US/docs/Web/JavaScript/Reference/… and developer.mozilla/en-US/docs/Learn/JavaScript/First_steps/… – Felix Kling Commented Oct 10, 2018 at 23:13
  • FWIW, strings are immutable in JavaScript, i.e. any operation you can do on a string produces a new string value that you have to assign to something. – Felix Kling Commented Oct 10, 2018 at 23:15
Add a ment  | 

6 Answers 6

Reset to default 2

You could use the map() method on the array type to achieve this:

var arr = ["abd","test","testss"];

// Iterate each string of arr, add a '.' to the start, 
// and return a copy of the updated array
arr = arr.map(function(value) { return '.' + value; })
    
console.log(arr)

If you're working with a fairly modern browser, you could also take advantage of modern syntax to achieve the same result via the following:

var arr = ["abd","test","testss"];

// Iterate each string of arr, add a '.' to the start, 
// and return a copy of the updated array
arr = arr.map(value => `.${value}`)
    
console.log(arr)

You can use Array.map API, like this -

arr= ["abd","test","testss"];

console.log(arr.map(item => `.${item}` ))

You can use array.map to do this. Array.map would let you travere the elements and returns an array. Here is the following snippet

let arr= ["abd","test","testss"];
arr = arr.map(item=> `.${item}`);
console.log(arr);

One way to do it is to use all the parameters provided by forEach, which are (1) the value of current element, (2) the index of the current element, (3) the array reference itself. Translated into code that would mean:

let arr = ['abc', 'def', 'ghi']

arr.forEach(function(value, index, array) {
    array[index] = '.' + value
});

// arr = ['.abc', '.def', '.ghi']

I am sure that there is a faster and possibly shorter way to do it, but hey it's something.

Assuming the strings don't have special chars, you can join and split.

let arr = ["abd", "test", "testss"];
console.log(("." + arr.join('|.')).split('|'));
.as-console-wrapper { max-height: 100% !important; top: 0; }

This is for backward patibility, without using transpilation.

var arr = ['a', 'b', 'c', 'd', 'e'];

for(var i = 0; i < arr.length; i++)
    arr[i] = '.' + arr[i];

console.log(arr); // ['.a', '.b', '.c', '.d', '.e']

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744744084a4591203.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信