Last updated: Mar 2, 2024
Reading timeยท4 min
To trim all strings in an array:
Array.map()
method to iterate over the array.String.trim()
method on each string and return the result.map()
method will return a new array containing only strings with the
whitespace from both ends removed.const arr = [' bobby ', ' hadz ', ' com ']; const results = arr.map(element => { return element.trim(); }); console.log(results); // ๐๏ธ [ 'bobby', 'hadz', 'com' ]
The function we passed to the Array.map() method gets called with each element in the array.
map()
method returns a new array containing the values the callback function returned.We called the String.trim() method on each string and returned the result.
The trim()
method removes the leading and trailing whitespace from a string.
console.log(' abc '.trim()); // ๐๏ธ 'abc' console.log(' abc '.trim().length); // ๐๏ธ 3
Array.map()
method doesn't change the contents of the original array, it returns a new array.If you have to do this often, define a reusable function.
function trimArray(array) { return array.map(element => element.trim()); } const arr = trimArray([' bobby ', ' hadz ', ' com ']); console.log(arr); // ๐๏ธ [ 'bobby', 'hadz', 'com' ]
The trimArray
function takes an array as a parameter and removes the leading
and trailing whitespace from all strings in the array.
If your array contains elements of other types, e.g. numbers or objects, add an
if
statement to check if the current element is a string before calling the
trim()
method.
const arr = [' a ', 2, ' b ', {}]; const results = arr.map(element => { if (typeof element === 'string') { return element.trim(); } return element; }); // ๐๏ธ ['a', 2, 'b', {}] console.log(results);
We check if the current element is of type string
before calling the
String.trim()
method.
Elements of other types get returned straight away, so they also get added to the new array.
The Array.map()
method doesn't change the array in place, it returns a new
array.
If you want to change the array in place, use the Array.forEach()
method.
forEach()
This is a three-step process:
Array.forEach()
method to iterate over the array.String.trim()
method to trim each string.trim()
method.const arr = [' bobby ', ' hadz ', ' com ']; arr.forEach((element, index) => { arr[index] = element.trim(); }); console.log(arr); // ๐๏ธ [ 'bobby', 'hadz', 'com' ]
The function we passed to the Array.forEach()
method gets called with each
element and index.
trim()
method with the element.The Array.forEach()
method changes the original array in place.
If your array might contain values of other types, use an if
statement to
check if the current value is a string before calling the String.trim()
method.
const arr = [' bobby ', 2, ' hadz ', {}, ' com ']; arr.forEach((element, index) => { if (typeof element === 'string') { arr[index] = element.trim(); } }); console.log(arr); // ๐๏ธ [ 'bobby', 2, 'hadz', {}, 'com' ]
We used the typeof
operator to check if each element is a string before
calling the String.trim()
method.
If the array element isn't a string, it remains unchanged.
Which approach you pick is a matter of personal preference. I'd use the
Array.map()
method as I find it quite direct and easy to read.
If you need to trim all strings in a two-dimensional array, call Array.map()
twice.
const arr = [ [' a ', ' b '], [' c ', ' d '], ]; const result = arr.map(subarray => subarray.map(str => str.trim()), ); console.log(result); // ๐๏ธ [ [ 'a', 'b' ], [ 'c', 'd' ] ]
We used the Array.map()
twice in the example.
The first call to the map()
method is used to iterate over the array and the
nested calls to iterate over the subarrays.
On each iteration, we call the trim()
method to remove the leading and
trailing whitespace from the current string.
If you need to flatten the two-dimensional array and remove the leading and
trailing whitespace, use the Array.flatMap()
method instead.
const arr = [ [' a ', ' b '], [' c ', ' d '], ]; const result = arr.flatMap(subarray => subarray.map(str => str.trim()), ); console.log(result); // ๐๏ธ [ 'a', 'b', 'c', 'd' ]
The
Array.flatMap()
method flattens the result that the map()
method returns.
for
loopYou can also use a basic for
loop to trim all strings in an array.
const arr = [' bobby ', ' hadz ', ' com ']; for (let index = 0; index < arr.length; index++) { arr[index] = arr[index].trim(); } console.log(arr); // ๐๏ธ [ 'bobby', 'hadz', 'com' ]
We used a for
loop to iterate over the array of strings.
On each iteration, we access the array element at the current index and trim the string.
The syntax for a basic for
loop is a bit verbose and in general the
forEach()
method should be preferred.