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
In JavaScript, you can use the Array.map[] method to iterate over all elements and then use the string methods to change the case of the elements.
Here is an example that demonstrates how to use the String.toUpperCase[]
method along with Array.map[]
to uppercase all elements in an array:
const names = ['Ali', 'Atta', 'Alex', 'John'];
const uppercased = names.map[name => name.toUpperCase[]];
console.log[uppercased];
// ['ALI', 'ATTA', 'ALEX', 'JOHN']
The toUpperCase[]
method converts a
string to uppercase letters without changing the original string.
To convert all elements in an array to lowercase, you can use another JavaScript method called String.toLowerCase[]
as shown below:
const names = ['Ali', 'Atta', 'Alex', 'John'];
const lowercased = names.map[name => name.toLowerCase[]];
console.log[lowercased];
// ['ali', 'atta', 'alex', 'john']
Both string methods work in all modern browsers, and IE6 and above. However, the Array.map[]
was introduced in ES6 and only supports IE9 and up. On the other hand,
arrow functions do not work in IE at all.
To support legacy browsers [IE9 and above], you should use the normal function instead:
const uppercased = names.map[function [name] {
return name.toUpperCase[];
}];
const lowercased = names.map[function [name] {
return name.toLowerCase[];
}];
To learn more about JavaScript arrays and how to use them to store multiple pieces of information in one single variable, take a look at this guide.
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.
Today, we’ll explain to you how to lowercase or uppercase all array values in JavaScript. We have several ways to convert the values but in this short article, we will talk about the two different ways. Using array map[] method, we can iterate over all elements of the array and convert the case of the
element as we want. Let’s take an example for better understanding. Convert an array values to LowerCase Here, we will use the string method toLowerCase[] along with an array Convert an
array values to UpperCase1. Using array map[] method
map[]
method to convert the all array values into the LowerCase.let names = ["John", "William", "Elijah", "Michael"];
let convertedNames = names.map[name => name.toLowerCase[]];
console.log[convertedNames];
// Output: ["john", "william", "elijah", "michael"]
The same way to convert the array values into UpperCase using the toUpperCase[] method.
let names = ["John", "William", "Elijah", "Michael"];
let convertedNames = names.map[name => name.toUpperCase[]];
console.log[convertedNames];
// Output: ["JOHN", "WILLIAM", "ELIJAH", "MICHAEL"]
2. Using for loop
Let’s do the same task using the for
loop.
Convert an array values to LowerCase
let names = ["John", "William", "Elijah", "Michael"];
let convertedNames = [];
for [let i = 0; i < names.length; i++] {
convertedNames[i] = names[i].toLowerCase[];
}
console.log[convertedNames];
// Output: ["john", "william", "elijah", "michael"]
Convert an array values to UpperCase
let names = ["John", "William", "Elijah", "Michael"];
let convertedNames = [];
for [let i = 0; i < names.length; i++] {
convertedNames[i] = names[i].toUpperCase[];
}
console.log[convertedNames];
// Output: ["JOHN", "WILLIAM", "ELIJAH", "MICHAEL"]
You can also use the below simple way if you have a large array of values.
let convertedNames = names.join['$$'].toLowerCase[].split['$$'];
let convertedNames = names.join['$$'].toUpperCase[].split['$$'];
I hope you find this article helpful.
Thank you for reading. Happy Coding..!!