How do I remove the space between words in an array using Javascript?

In the following array, how can I remove spaces between words within each line? I want to convert "FLAT RATE" to "FLATRATE" and "FREE SHIPPING" to "FREESHIPPING".

enter image description here

I had to work with an array. I have seen solutions for a simple inline frame.

+3


source to share


5 answers


the string can be split and concatenated like this:

s.split(" ").join("");

      



This removes spaces.

+3


source


You can use a function array.map

to loop through an array and use regex to remove all the space:



var array = ['FLAT RATE', 'FREE SHIPPING'];

var nospace_array = array.map(function(item){
	return item.replace(/\s+/g,'');
})

console.log(nospace_array)
      

Run codeHide result


+3


source


 ['FLAT RATE', 'FREE SHIPPING'].toString().replace(/ /g,"").split(",")
      

Run codeHide result


I admit: not the best answer as it relies on array strings to not contain a comma.

.map is really the way to go, but since that was already given and since I like chaining I gave another (quick and dirty) solution

+1


source


You don't really need jQuery.

var ListOfWords = ["Some Words Here", "More Words Here"];
for(var i=0; i < ListOfWords.length; i++){
    ListOfWords[i] = ListOfWords[i].replace(/\s+/gmi, "");
}
console.log(ListOfWords);

      

0


source


You can use the replace function to accomplish this.

var shipping = ["FLAT RATE", "FREE SHIPPING"];

var without_whitespace = shipping.map(function(str) {
   replaced = str.replace(' ', ''); return replaced;
});

      

0


source







All Articles