JavaScript - how to trim spaces for all strings of an array

If there is a situation when you need to get rid of spaces at the beginning and end of the string for array elements, then it could be done in this way:

let testArray = ['Riga', 'Moscow ', ' London '];
let resultArray = testArray.map(str => str.trim());
console.log(resultArray); // Result: ['Riga', 'Moscow', 'London']

Comments

No comments yet, you can leave yours: