Your attempts didn't work because a) arrays don't have a toUpperCase method and b) there is no toUpperCase function.
Commented Apr 18, 2015 at 15:46 this works: array2.map(fruit => fruit.toUpperCase()) Commented Jan 28, 2022 at 0:45you should use handler function in map:
var array2 = ["melon","banana","apple","orange","lemon"]; array2 = array2.map(function(x)< return x.toUpperCase(); >)
edit: yes you can do
toUpper = function(x)< return x.toUpperCase(); >; array2 = array2.map(toUpper);
5,415 6 6 gold badges 37 37 silver badges 73 73 bronze badges
answered Apr 18, 2015 at 15:34
Jose Ricardo Bustos M. Jose Ricardo Bustos M.
8,136 7 7 gold badges 42 42 silver badges 64 64 bronze badges
Thanks for the help! When using .map( function(x). Should I always define a function then? Or can I use already existing? If yes, how do I write it then? Thanks
Commented Apr 18, 2015 at 15:39 @BjornRunow: you just have to pass a function. .map doesn't care where it comes from. Commented Apr 18, 2015 at 15:43 @BjornRunow check post Commented Apr 18, 2015 at 15:45new_array = array2.map(function(x)< return x.toUpperCase() >); (map returns a new array, but the example suggests it changes the original array)
Commented Aug 9, 2017 at 6:06 the answer is wrong :( see Marcel's answer: stackoverflow.com/a/29719380/1786393 Commented Feb 14, 2019 at 12:35toUpperCase() do not change the value. Try
array2 = array2.map(function (e) < return e.toUpperCase() >);
4,005 2 2 gold badges 15 15 silver badges 25 25 bronze badges
answered Apr 18, 2015 at 15:37
3,092 1 1 gold badge 19 19 silver badges 27 27 bronze badges
Marcel is right. His answer is the correct one. You need to be able to store the map function in another variable, and not try to mutate the original array as @eddyparkinson points out as well.
Commented Feb 21, 2018 at 14:22you can use this :
var a = ['this','is','test']; a.map(f=>< return f.toUpperCase(); >);
answered Aug 26, 2018 at 12:04
47 1 1 silver badge 5 5 bronze badges
You're using ES6. return + braces is unnecessary.
Commented Jan 31, 2022 at 13:46
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.
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.
arr = ["fulu", "nulu", "hulu"]; var arrToUp = String.prototype.toUpperCase.apply(arr).split(","); console.log(arrToUp) //["FULU", "NULU", "HULU"]
answered Aug 21, 2016 at 2:09
577 5 5 silver badges 14 14 bronze badges
OK, but you better don't have any coma in your data.
Commented Oct 28, 2016 at 21:36
fail in case, arr = ["fu,lu", "nulu", "hulu"]
Commented May 6, 2021 at 11:22
Try looping through each element and making it upper case.
for(var i = 0; i4,408 3 3 gold badges 30 30 silver badges 37 37 bronze badges answered Apr 18, 2015 at 15:37 351 1 1 silver badge 8 8 bronze badges - for using int in javascript Commented Jun 21, 2017 at 10:55
You can add the method toUpperCase() to Array.prototype:
Array.prototype.toUpperCase = function() < return this.map( (el) =>(typeof el === "string") ? el.toUpperCase() : el)>
answered Jul 23, 2017 at 20:48
11 1 1 bronze badge
This might be the simplest solution without having to loop through the array.
const yourArray = ["apple","banana"]; const upperCasedArray = String(yourArray).toUpperCase().split(","); //OR const upperCasedArray = yourArray.toString().toUpperCase().split(",");
What's happening is first we convert the array into a string then use the toUpperCase() method of string to make that string all uppercased then split it by using "," so we get a new array which is uppercased.
answered May 21, 2021 at 17:05 Basanta Kc Basanta Kc 265 4 4 silver badges 5 5 bronze badgesJavaScript does not really support a toUpperCase() function for arrays, as far as I know. You have to create a for or foreach loop to loop through the array and call .toUpperCase() on each element in the array.
You could also create a little function like so: other question on StackOverflow
1 1 1 silver badge answered Apr 18, 2015 at 15:37 jbehrens94 jbehrens94 2,396 6 6 gold badges 32 32 silver badges 59 59 bronze badgesvar arr = ["hamed","hassan","hema","zeiad","saad","omar","ali","sayed","ahmed"]; var str = String(arr).toUpperCase().split(",");
11.2k 16 16 gold badges 63 63 silver badges 85 85 bronze badges
answered May 19, 2017 at 7:25
Ahmed Abaza Ahmed Abaza
9 1 1 bronze badge
fail in case, arr = ["ha,med","hassan","hema","zeiad","saad","omar","ali","sayed","ahmed"]
Commented May 6, 2021 at 11:24
If you're using Underscore.js and cannot use ES6 niceties then it's something like this:
_.map( array2, function( value ) < return value.toUpperCase() >)
answered Dec 30, 2018 at 1:22
Joshua Pinter Joshua Pinter
47k 23 23 gold badges 254 254 silver badges 248 248 bronze badges
why would anyone replace totally native functionally with a huge library
Commented Jun 23, 2019 at 23:38
@LukeRobertson If you have access to .map natively, go for it. At the time of writing, the .map function was not a native method without ES6.
Commented Jun 23, 2019 at 23:57Easily you can convert into uppercase or lowercase .
var my_array = ["melon","banana","apple","orange","lemon"]; function convertToUppercase() < var e = document.getElementById('show'); e.innerHTML = "Uppercase: " + String(my_array).toUpperCase().split(","); console.log(String(my_array).toUpperCase().split(",")); >function convertToLowercase()
button < background: #0095ff; color: white; border: none; border-radius: 3px; padding: 8px; margin: 10px; cursor: pointer; -moz-box-shadow: 0 0 8px #999; -webkit-box-shadow: 0 0 8px #999; box-shadow: 0 0 8px #999; >p
answered Jan 19, 2021 at 6:12
Rohit Tagadiya Rohit Tagadiya
3,658 1 1 gold badge 26 26 silver badges 31 31 bronze badges
fail in case, my_array = ["me,lon","banana","apple","orange","lemon"]
Commented May 6, 2021 at 11:26
var array2 = ["melon","banana","apple","orange","lemon"]; array2.map(fruit => fruit.toUpperCase())
answered Jan 28, 2022 at 0:47
hanumanDev hanumanDev
6,594 13 13 gold badges 85 85 silver badges 148 148 bronze badges
You are trying to change the values of an array. To do so, you have to iterate through the array first before applying your desired method on the array. That way, each value will take the applied method.
const array2 = ["melon","banana","apple","orange","lemon"]; /*create another variable (newArr) which would store your "loop through array2"*/ const newArr = array2.map(arr => arr.toUpperCase()); console.log(newArr);
29.6k 11 11 gold badges 95 95 silver badges 104 104 bronze badges
answered May 2, 2022 at 5:47
Chukwuemeka Chukwuemeka
56 1 1 silver badge 5 5 bronze badges
To recap :
There are 2 main methods to do it :
1. Using the modern Javascript ES6 version with arrow function and map() iterator :
// the test array const items = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6']; // declare the function with arrow function using the map iterator and the // toUpperCase() buildin method const countItems = arr => arr.map(item => item.toUpperCase());
2. Using the old good function style with a simple for loop, the toUpperCase() buildin method and the push() method
// the test array const items = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6']; // the old way function countItems(arr) < newarray = []; for (let i = 0; i < arr.length; i++) < newarray.push(arr[i].toUpperCase()); >return newarray; >;
3. call the function with any of the above snippets
console.log(countItems(items));
[ 'ITEM1', 'ITEM2', 'ITEM3', 'ITEM4', 'ITEM5', 'ITEM6' ]
hope that this answer adds a small stone to whatever you want to build