Trim All Strings In A JavaScript Array

To trim all strings in a JavaScript array, use the .map() method. The map() method calls the trim() method on each element of the array, and returns a new array with the trimmed elements.

For example, given an array of strings:

var arr = [" one ", " two ", " three "];

Calling `arr.map(function(str){ return str.trim(); })` would return a new array:

["one", "two", "three"];
trim all strings in a javascript array

map() Method To Trim All Strings In A JavaScript Array

You can use the Array.map() method to trim all strings in a JavaScript array. The map() method calls the trim() method on each element of the array, and returns a new array with the trimmed elements.

For example, given an array of strings:

var arr = [" one ", " two ", " three "];

var newArr = arr.map(function(str){

  return str.trim();

});

console.log(newArr);

The newArr variable would now contain:

[“one”, “two”, “three”];

In the above method, we first created an array arr with three strings. We then called the map() method on this array, and passed in a callback function that trims each string.

The map() method calls the callback function for each element in the array, and returns a new array with the results. In our case, the new array contains the trimmed strings from the original array.

We then stored this new array in the newArr variable, and logged it to the console.

Note that you can also use arrow functions when passing a callback function to map(). For example:

var arr = [" one ", " two ", " three "];

var newArr = arr.map(str => str.trim());

console.log(newArr);

The output would be the same as in the previous example.

If the array contains different types of elements, you can use the typeof operator to check if an element is a string before trimming it. For example:

var arr = [" one ", " two ", 2, true];

var newArr = arr.map(function(element){

if (typeof element === "string") {

  return element.trim();

}

return element;

});

console.log(newArr); //["one", "two", 2, true]

In the above code, we first created an array arr that contains strings and non-strings. We then called the map() method on this array, and passed in a callback function.

This callback function checks if each element is a string using the typeof operator. If it is a string, it trims the element and returns it. Otherwise, it just returns the element.

The map() method calls this callback function for each element in the array, and returns a new array with the results. In our case, the new array contains trimmed strings and non-strings from the original array.

We then stored this new array in the newArr variable and logged it to the console.

forEach() Method To Trim All Strings In A JavaScript Array

You can also use the forEach() method to trim all strings in an array.

For example:

var arr = [" one ", " two ", " three "];

var arr1 = [];

arr.forEach(function(element, index){ 

  arr1.push(element.trim());

});

console.log(arr1); //["one", "two", "three"]

In the above code, we first created an array arr that contains three strings. We then called the forEach() method on this array, and passed in a callback function.

This callback function trims each string in the array, and assigns it to the corresponding index of a new array (arr1).

The forEach() method calls this callback function for each element in the array, and assigns the trimmed string to the arr1 array.

We then stored this new array in the arr1 variable and logged it to the console.

Note that you can also use arrow functions when passing a callback function to forEach(). For example:

var arr = [" one ", " two ", " three "];

var arr1 = [];

arr.forEach((element, index) => { 

  arr1.push(element.trim());

});

console.log(arr1); //["one", "two", "three"]

The output would be the same as in the previous example.

If the array contains different types of elements, you can use the typeof operator to check if an element is a string before trimming it. For example:

var arr = [" one ", " two ", 2, true];

var arr1 = [];

arr.forEach(function(element, index){ 

if (typeof element === "string") {

   arr1.push(element.trim());

} else{

   arr1.push(element);

}

});

console.log(arr1); //["one", "two", 2, true]

In the above code, we first created an array arr that contains strings and non-strings. We then called the forEach() method on this array, and passed in a callback function.

This callback function checks if each element is a string using the typeof operator. If it is a string, it trims the element and assigns it to the corresponding index of a new array (arr1). Otherwise, it just assigns the element to that index.

The forEach() method calls this callback function for each element in the array, and assigns the trimmed string or non-string to the arr1 array.

We then stored this new array in the arr1 variable and logged it to the console.

reduce() Method To Trim All Strings In A JavaScript Array

You can also use the reduce() method to trim all strings in an array.

For example:

var arr = [" one ", " two ", " three "];

var newArr = arr.reduce(function(accumulator, currentValue){ 

  return accumulator.concat(currentValue.trim());

}, []);

console.log(newArr); //["one", "two", "three"]

In the above code, we first created an array arr that contains three strings. We then called the reduce() method on this array, and passed in a callback function.

This callback function trims each string in the array, and concatenates it to the accumulator array. The reduce() method then assigns this new array to the newArr variable and logs it to the console.

If the array contains different types of elements, you can use the typeof operator to check if an element is a string before trimming it. For example:

var arr = [" one ", " two ", 2, true];

var newArr = arr.reduce(function(accumulator, currentValue){ 

if (typeof currentValue === "string") {

   return accumulator.concat(currentValue.trim());

} else{

   return accumulator.concat(currentValue);

}

}, []);

console.log(newArr); //["one", "two", 2, true]

In the above code, we first created an array arr that contains strings and non-strings. We then called the reduce() method on this array, and passed in a callback function.

This callback function checks if each element is a string using the typeof operator. If it is a string, it trims the element and concatenates it to the accumulator array. Otherwise, it just concatenates the element to the array.

The reduce() method then assigns this new array to the newArr variable and logs it to the console.

The output would be the same as in the previous example.

Conclusion

In this article, you learned how to trim all strings in a JavaScript array using different methods. You can use the map() method to create a new array that contains the trimmed strings, or you can use the forEach() or reduce() method to trim all strings.

Remember that you can also use arrow functions when passing callback functions to these methods.

Leave a Reply