最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery strip spaces out of array - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited May 23, 2017 at 12:06 CommunityBot 11 silver badge asked Feb 5, 2015 at 21:19 NateWNateW 2,1313 gold badges29 silver badges39 bronze badges 2
  • Try not to use any jQuery at all for this task, and it will work. – Bergi Commented Feb 5, 2015 at 21:20
  • @Bergi is it faster to not use jQuery? – NateW Commented Feb 5, 2015 at 21:59
Add a ment  | 

5 Answers 5

Reset to default 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

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);

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(' ', ''));
}

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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

$(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 remend using some of the other solutions here. $.each isn't the best option, you want to map.

发布评论

评论列表(0)

  1. 暂无评论