How to uppercase an array in javascript

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.

  1. Using array map[] method
  2. Using for loop

1. Using array map[] method

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 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"]

Convert an array values to UpperCase

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..!!

How do you capitalize letters in an array?

To capitalize the first letter of each word in an array: Use the map[] method to iterate over the array. On each iteration, use the toUpperCase[] method on the first character of the word and concatenate the rest. The map method will return a new array with all words capitalized.

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 can I uppercase a string in JavaScript?

The toUpperCase[] method returns the value of the string converted to uppercase. This method does not affect the value of the string itself since JavaScript strings are immutable.

Chủ Đề