Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert array into upper case

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!

like image 722
BjornRunow Avatar asked Apr 18 '15 15:04

BjornRunow


People also ask

How do I change an array to lowercase?

To convert all array elements to lowercase:Use the map() method to iterate over the array. On each iteration, call the toLowerCase() method to convert the string to lowercase and return the result. The map method will return a new array containing only lowercase strings.

How do you convert all strings to title caps in a string array?

Convert all the elements in each and every word in to lowercase using string. toLowerCase() method. Loop through first elements of all the words using for loop and convert them in to uppercase.

How do you uppercase a function in Javascript?

The toUpperCase() method converts a string to uppercase letters. The toUpperCase() method does not change the original string.

What is the correct way to convert a string into upper case?

Java String toUpperCase() Method The toUpperCase() method converts a string to upper case letters.


1 Answers

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); 
like image 122
Jose Ricardo Bustos M. Avatar answered Sep 29 '22 08:09

Jose Ricardo Bustos M.