37

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!

BjornRunow
  • 525
  • 1
  • 4
  • 10

15 Answers15

81

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);
Beginner
  • 5,277
  • 6
  • 34
  • 71
Jose Ricardo Bustos M.
  • 8,016
  • 6
  • 40
  • 62
8

toUpperCase() do not change the value. Try

array2 = array2.map(function (e) { 
    return e.toUpperCase()
});
Mech
  • 3,952
  • 2
  • 14
  • 25
marcel
  • 2,967
  • 1
  • 16
  • 25
  • 2
    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. – Maria Campbell Feb 21 '18 at 14:22
4

you can use this :

var a = ['this','is','test'];

a.map(f=>{ return f.toUpperCase(); });
saeid
  • 37
  • 5
3

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"]
dannymac
  • 517
  • 5
  • 14
2

Try looping through each element and making it upper case.

for(var i = 0; i < array2.length; i++){
    array2[i] = array2[i].toUpperCase();
}
psulek
  • 4,308
  • 3
  • 29
  • 37
Dan12-16
  • 351
  • 1
  • 8
1

You can add the method toUpperCase() to Array.prototype:

Array.prototype.toUpperCase = function(){
   return this.map( (el) => (typeof el === "string") ? el.toUpperCase() : el)}
amy0
  • 11
  • 1
1

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.

Basanta Kc
  • 245
  • 4
  • 5
0

JavaScript 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

Community
  • 1
  • 1
jbehrens94
  • 2,356
  • 6
  • 31
  • 59
0
var arr = ["hamed","hassan","hema","zeiad","saad","omar","ali","sayed","ahmed"];
var str = String(arr).toUpperCase().split(",");
wscourge
  • 10,657
  • 14
  • 59
  • 80
0

Underscore.js

If you're using Underscore.js and cannot use ES6 niceties then it's something like this:

_.map( array2, function( value ){ return value.toUpperCase() } )
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
0

Easily 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() {
  console.clear();
  var e = document.getElementById('show');
  e.innerHTML = "Lowercase: " +  String(my_array).toLowerCase().split(",");
  console.log(String(my_array).toLowerCase().split(","));
}
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 {
  margin: 0;
}
<button onclick="convertToUppercase()">Convert to Uppercase</button>
<button onclick="convertToLowercase()">Convert to Lowercase</button>
<p id="show"></p>
Rohit Tagadiya
  • 3,373
  • 1
  • 25
  • 25
0
var array2 = ["melon","banana","apple","orange","lemon"];

array2.map(fruit => fruit.toUpperCase())
hanumanDev
  • 6,592
  • 11
  • 82
  • 146
0

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

QED

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Chukwuemeka
  • 56
  • 1
  • 5
0

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

// Should print

[ 'ITEM1', 'ITEM2', 'ITEM3', 'ITEM4', 'ITEM5', 'ITEM6' ]

hope that this answer adds a small stone to whatever you want to build

Gizmapps
  • 59
  • 1
  • 10
-2
var name = ['Amit','Sumit','Kumit'];
var newname = name.join('-').toUpperCase().split();
console.log(newname);
slfan
  • 8,950
  • 115
  • 65
  • 78