Sometimes, we want to apply trim function to each string in an array with JavaScript.
In this article, we’ll look at how to apply trim function to each string in an array with JavaScript.
How to apply trim function to each string in an array with JavaScript?
To apply trim function to each string in an array with JavaScript, we can use the array map
method.
For instance, we write
const trimmedArr = arr.map((s) => s.trim());
to call arr.map
with a callback that returns the trimmed version of each string we get from calling s.trim
.
And then we assign the returned array to trimmedArr
.
Conclusion
To apply trim function to each string in an array with JavaScript, we can use the array map
method.