I have an array that I need to remove spaces from, for example it returns like
[book, row boat,rain coat]
However, I would like to remove all the white spaces.
All the guides I saw online said to use .replace, but it seems like that only works for strings. Here is my code so far.
function trimArray(wordlist)
{
for(var i=0;i<wordlist.length;i++)
{
wordlist[i] = wordlist.replace(/\s+/, "");
}
}
I have also tired replace(/\s/g, '');
Any help is greatly appreciated!
wordlist[i] = wordlist[i].replace(/\s+/, "");. Try using something like map, it's a much cleaner approach. Note that your replacement function will replace any whitespace. If you want to just trim, use thetrimfunction:wordlist[i] = wordlist[i].trim()[book, row boat,rain coat]- Doesn't look like a string array. Is this one string or are you getting back[" book", " row", " boat", " coat"]["cat food","dogs ","fish tank"]do you want the spaces withincat foodandfish tankgone too? Or will there never be any spaces within the strings?