19

I'm interested if there is any function like array_map or array_walk from php.

Don't need an for that travels all the array. I can do that for myself.

var array = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
// i would like something like this if exists - function(array, 'upperCase');
user1236048
  • 5,542
  • 7
  • 50
  • 87

10 Answers10

23

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();
});
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
11

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
nkron
  • 19,086
  • 3
  • 39
  • 27
8

This may be an overkill but you can also do

var upperCaseArray = array.map(String.prototype.toUpperCase.call.bind(String.prototype.toUpperCase));
Sharat M R
  • 335
  • 3
  • 8
  • @nkron's answer is similar to this, but less verbose. – Ceasar Dec 09 '15 at 19:05
  • 2
    That code is a bit confusing because the `String.prototype.toUpperCase.call` part of the code is just accessing the [Function's call](http://www.javascripture.com/Function#call) method. That method is the same for all Functions so you can stick any Function to the left of .call without changing the behavior. I'd suggest changing it to `Function.prototype.call...` to be explicit that you are using Function's call, not a special feature of the toUpperCase function. – nkron Apr 13 '16 at 20:49
  • The clearer example is: `['a','b','c'].map(Function.prototype.call.bind(String.prototype.toUpperCase))` – Ivan Hamilton May 04 '16 at 01:50
5

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(",");
Endless
  • 34,080
  • 13
  • 108
  • 131
5

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

Sujit Agarwal
  • 12,348
  • 11
  • 48
  • 79
4

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(); });
stusmith
  • 14,003
  • 7
  • 56
  • 89
3

map() is somehow similar to array_walk

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

mamoo
  • 8,156
  • 2
  • 28
  • 38
2

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

Chetter Hummin
  • 6,687
  • 8
  • 32
  • 44
1

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(',')
james emanon
  • 11,185
  • 11
  • 56
  • 97
  • wow.... care to explain how it's working. how/why is apply converting array to string – aWebDeveloper Jun 09 '16 at 10:12
  • sure. Since I wanted to 'toUpperCase', I needed to be able to delegate to it, or borrow that functionality. Since, I needed an array to do it, I had to somehow pass the "context" IN to the "toUpperCase". apply (since we are using an array), allows this. because I didn't pass a second arg, the first arg "array", is the context. So basically, it spreads it out for me... then I split it to turn it back into an array. – james emanon Jun 09 '16 at 23:09
0

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']);