how to map an array with uppercase function in javascript?

安稳与你 提交于 2019-12-03 10:08:58

You can use $.map() in order to apply String.toUpperCase() (or String.toLocaleUpperCase(), if appropriate) to your array items:

var upperCasedArray = $.map(array, String.toUpperCase);

Note that $.map() builds a new array. If you want to modify your existing array in-place, you can use $.each() with an anonymous function:

$.each(array, function(index, item) {
    array[index] = item.toUpperCase();
});

Update: As afanasy rightfully points out in the comments below, mapping String.toUpperCase directly will only work in Gecko-based browsers.

To support the other browsers, you can provide your own function:

var upperCasedArray = $.map(array, function(item, index) {
    return item.toUpperCase();
});

Check out JavaScript Array.map for info on the official JavaScript map function. You can play around with the sample there to see how the function works. Try copying this into the example box:

var array = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
var uppers = array.map(function(x) { return x.toUpperCase(); });
console.log(uppers);

And it will print:

DOM,LUN,MAR,MER,GIO,VEN,SAB

This may be an overkill but you can also do

var upperCaseArray = array.map(String.prototype.toUpperCase.call.bind(String.prototype.toUpperCase));

You can implement a function for it:

Array.prototype.myUcase=function()
{
  for (i=0;i<this.length;i++)
    {
    this[i]=this[i].toUpperCase();
    }
}

USAGE

var fruits=["Banana","Orange","Apple","Mango"];
fruits.myUcase();

RESULT

BANANA,ORANGE,APPLE,MANGO 

Reference LINK

You could consider using the Underscore.js library which provides standard functional operations.

Then the code would be as simple as:

_.map(array, function (x) { return x.toUpperCase(); });

map() is somehow similar to array_walk

http://jqapi.com/#p=map

And here is the one-liner without creating a loop or a function that is ES3 compatible It's alos faster since you only call toUpperCase once

// watch out for strings with comma (eg: ["hi, max"]) then use custom join/split()
// or just use something safer such as Array.map()
var uppercaseArray = array.toString().toUpperCase().split(",");
var uppercaseArray = array.join(0).toUpperCase().split(0);
var uppercaseArray = (array+"").toUpperCase().split(",");

Javascript has a map() method. A good reference is at http://www.tutorialspoint.com/javascript/array_map.htm

I understand it is a little late to the party, but I wanted to add this here as another Delegation way of doing it.

var array = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];

"".toUpperCase.apply(array).split(',')

Similar to a previous for loop answer, this one uses some of the more basic features of javascript:

function looptoUpper(arr){
    var newArr = [];
    for (var i = 0; i < arr.length; i++){
        newArr.push(arr[i].toUpperCase());
    }
    return newArr;
}
looptoUpper(['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab']);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!