admin管理员组文章数量:1394053
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
6 Answers
Reset to default 2You 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']
本文标签: How to append a dot at the beginning of a string in arrayJavascriptStack Overflow
版权声明:本文标题:How to append a dot at the beginning of a string in array-Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744744083a2622784.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论