In the below array, how can I remove whitespace between words within each string? I want to convert "FLAT RATE" to "FLATRATE" and "FREE SHIPPING" to "FREESHIPPING".
I had to work out with array. I saw the solutions for simple string's case.
In the below array, how can I remove whitespace between words within each string? I want to convert "FLAT RATE" to "FLATRATE" and "FREE SHIPPING" to "FREESHIPPING".
I had to work out with array. I saw the solutions for simple string's case.
a string can be split and joined this way:
s.split(" ").join("");
That removes spaces.
['FLAT RATE', 'FREE SHIPPING'].toString().replace(/ /g,"").split(",")
I admit : not the best answer, since it relies on the array strings not to contain a comma.
.map is indeed the way to go, but since that was already given, and since I like chaining, I gave another (quick and dirty) solution
You can use the replace function to achieve this.
var shipping = ["FLAT RATE", "FREE SHIPPING"];
var without_whitespace = shipping.map(function(str) {
replaced = str.replace(' ', ''); return replaced;
});
"FLAT RATE AMOUNT" => "FLATRATE AMOUNT".return item.replace(/\s+/g,'');