Javascript 将数组转换为大写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29719329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert array into upper case
提问by BjornRunow
I have an array that I want to change to upper case but I can′t make it work. Please help me.
我有一个数组,我想将其更改为大写,但我无法使其工作。请帮我。
var array2 = ["melon","banana","apple","orange","lemon"];
array2.toUpperCase()
I have also tried the below but it doesn′t work neither.
我也试过下面的但它也不起作用。
array2.map(toUpperCase);
Thanks in advance!
提前致谢!
回答by Jose Ricardo Bustos M.
you should use handler function in map:
您应该在地图中使用处理程序函数:
var array2 = ["melon","banana","apple","orange","lemon"];
array2 = array2.map(function(x){ return x.toUpperCase() })
for more information about map
edit: yes you can do
编辑:是的,你可以
toUpper = function(x){
return x.toUpperCase();
};
array2 = array2.map(toUpper);
回答by marcel
toUpperCase()do not change the value.
Try array2 = array2.map(function (e) {
return e.toUpperCase()
});
toUpperCase()不要更改值。尝试array2 = array2.map(function (e) {
return e.toUpperCase()
});
回答by dannymac
Map is the way I'd recommend, but here's an alternate that may possibly help someone wishing to understand apply used on an objects prototype.
Map 是我推荐的方式,但这里有一个替代方法,可能会帮助那些希望了解应用在对象原型上的人。
This is an example of an array borrowing the String objects .toUpperCase() method with .apply() and using it on said array. The array is coerced to a string using .apply() and then back to an uppercased array using the .split() method.
这是一个使用 .apply() 借用 String 对象 .toUpperCase() 方法并在所述数组上使用它的数组示例。使用 .apply() 将数组强制转换为字符串,然后使用 .split() 方法将其强制转换为大写数组。
arr = ["fulu", "nulu", "hulu"];
var arrToUp = String.prototype.toUpperCase.apply(arr).split(",");
console.log(arrToUp)
//["FULU", "NULU", "HULU"]
回答by saeid
you can use this :
你可以使用这个:
var a = ['this','is','test'];
a.map(f=>{ return f.toUpperCase(); });
回答by Ahmed Abaza
var arr = ["hamed","hassan","hema","zeiad","saad","omar","ali","sayed","ahmed"];
var str = String(arr).toUpperCase().split(",");
回答by amy0
You can add the method toUpperCase() to Array.prototype:
您可以将 toUpperCase() 方法添加到 Array.prototype:
Array.prototype.toUpperCase = function(){
return this.map( (el) => (typeof el === "string") ? el.toUpperCase() : el)}
回答by Dan12-16
Try looping through each element and making it upper case.
尝试遍历每个元素并使其大写。
for(var i = 0; i < array2.length; i++){
array2[i] = array2[i].toUpperCase();
}
回答by jbehrens94
JavaScript does not really support a toUpperCase()function for arrays, as far as I know. You have to create a foror foreachloop to loop through the array and call .toUpperCase()on each element in the array.
toUpperCase()据我所知,JavaScript 并不真正支持数组函数。您必须创建一个fororforeach循环来遍历数组并调用数组中的.toUpperCase()每个元素。
You could also create a little function like so: other question on StackOverflow
你也可以像这样创建一个小函数:关于 StackOverflow 的其他问题
回答by Joshua Pinter
Underscore.js
下划线.js
If you're using Underscore.js and cannot use ES6 niceties then it's something like this:
如果你使用 Underscore.js 并且不能使用 ES6 的细节,那么它是这样的:
_.map( array2, function( value ){ return value.toUpperCase() } )
回答by Amit Rai
var name = ['Amit','Sumit','Kumit'];
var newname = name.join('-').toUpperCase().split();
console.log(newname);

