Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove white space between words in an array using Javascript?

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".

enter image description here

I had to work out with array. I saw the solutions for simple string's case.

like image 620
sagar sapkota Avatar asked Sep 25 '26 01:09

sagar sapkota


2 Answers

You can use array.map function to loop in array and use regex to remove all space:

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

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

console.log(nospace_array)
like image 161
Mohammad Hamedani Avatar answered Sep 27 '26 15:09

Mohammad Hamedani


a string can be split and joined this way:

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

That removes spaces.

like image 30
pid Avatar answered Sep 27 '26 13:09

pid