![](/img/trans.png)
[英]how to get the first and the last character of a string using a Regex and Javascript
[英]How to get and return each last character of array to be in uppercase using javascript?
我有以下array
变量,我想使用javascript返回给定的数组,其中每个元素的最后一个字符都是Uppercase
的,我尝试过如下所示,但不幸的是我得到了undefined
,请你帮我得到想要的输出如下。 提前致谢。
期望的输出: [onE, twO, threE, fouR]
var arr = ['one', 'two', 'three', 'four'];
var res = arr.map((item) => {
item.substr(arr.length) + item.charAt(arr.length -1).toUpperCase();
});
console.log(res);//[undefined, undefined, undefined, undefined]
您缺少退货声明:
var res = arr.map((item) => {
return item.slice(0,-1)+item.slice(-1).toUpperCase()
});
您还可以跳过大括号并返回关键字:
const arr = ['one', 'two', 'three', 'four'];
const res = arr.map(item =>
item.substring(0, item.length - 1) +
item.charAt(item.length - 1).toUpperCase()
)
console.log(res);
另请注意, String.prototype.substr被提及为已弃用(您可以使用 substring 代替)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.