1

I'm trying to strip all the spaces out of the contents of my array.

Here is my array

var array = ["option 1", "option 2", "option 3"]

I've tried using the answer found here: how do I strip white space when grabbing text with jQuery?

This is what the jQuery I'm trying to use.

$(array1).each(function(entry) {
  $(this).replace(/\s+/g, '');
  console.log(entry);
});

But it throws a TypeError: undefined is not a function (evaluating 'entry.replace(/\s+/g, '')')

What am I missing?

2
  • Try not to use any jQuery at all for this task, and it will work. Commented Feb 5, 2015 at 21:20
  • @Bergi is it faster to not use jQuery? Commented Feb 5, 2015 at 21:59

5 Answers 5

5

You can use map to make a new array.

In the map function, you use the regexp on your value.


jQuery

array = $.map(array, function(value){
  return value.replace(/ /g, '');
});

Fiddle;


Vanilla JS version

array = array.map(function(value){
  return value.replace(/ /g, '');
});

Fiddle


Old IE vanilla JS

for(var i=0; i<array.length; i++){
  array[i] = array[i].replace(/ /g, '');
};

Fiddle


No loop universal method (may have conflict with characters)

array = array.join('$').replace(/ /g, '').split('$');

Fiddle

Sign up to request clarification or add additional context in comments.

4 Comments

Notice that your "old IE" version does something different than the map solutions
@Bergi You lost me there, what do you mean?
The one modifies the array object, the other replaces the array variable.
@Bergi Oh ok, yeah indeed it does then!
1

No need to use jQuery here, just go with plain array methods like map or simple for-loop:

var array1 = ["option 1", "option 2", "option 3"].map(function(el) {
    return el.replace(/\s*/g, '')
});

document.write(array1);

Comments

0

This simple for loop, loops through the array items and removes any spaces no jQuery or regular expressions needed.

var arr = ["option 1", "option 2", "option 3"];

for (var i = 0; i < arr.length; i++) {
    alert(arr[i].replace(' ', ''));
}

3 Comments

However, whitespace (/\s+/) is something different than blanks (` `)
could you provide me an example?
Simple: var arr = ["option\x0a1", "option\t2", "option\n3"]
0

Take a look at how 'each' works in jQuery, it is not quite what you think. This code can be fixed by noting that the first parameter to your callback is the index into the array, and so you can adjust the code like so.

var array1 = ["option 1", "option 2", "option 3"]

$(array1).each(function(entry, value) {
  array1[entry] = value.replace(/\s+/g, '');
  console.log(array1[entry]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

1 Comment

$(array1).each(function(a, b) { array1[a] = b.replace(/\s+/g, ''); });
0

$(this) is not what you want, you want entry, that's your string

entry = entry.replace(/\s+/g, '');

Also actually your string is the second argument. So you'd need to change your function to function(key, entry) also

That's why it doesn't work, but I recommend using some of the other solutions here. $.each isn't the best option, you want to map.

2 Comments

Entry is an array index. First parameter past to function is index second is element.
Sorry, didn't realize jQuery was so weird.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.