jQuery .each help,我想修剪()数组中的所有字符串

时间:2021-06-04 13:38:18

I'm splitting a string into an array, then I want to remove the white space around each element. I'm using jQuery. I'm able to do this successfully with 2 arrays but I know it's not correct. How do I loop thru an array and trim each element so the elements keep that change. Thanks for any tips. Here is my working code using two array. Please show me the correct way to do this.

我正在将一个字符串拆分成一个数组,然后我想删除每个元素周围的空白区域。我正在使用jQuery。我能够成功地使用2个阵列,但我知道它不正确。如何通过数组循环并修剪每个元素,以便元素保持这种变化。谢谢你的任何提示。这是我使用两个数组的工作代码。请告诉我正确的方法。

var arVeh = vehicleText.split("|");
    var cleanArry = new Array();
    $.each(arVeh, function (idx, val) {

        cleanArry.push($.trim(this));

    });

Cheers, ~ck in San Diego

干杯,〜在圣地亚哥

8 个解决方案

#1


50  

You don't even really need the idx or val parameters. This appears to work on jsFiddle:

你甚至不需要idx或val参数。这似乎适用于jsFiddle:

var cleanVehicles = [];

$.each(vehicleText.split("|"), function(){
    cleanVehicles.push($.trim(this));
});

EDIT: Now that I've seen what you're really after, try using map:

编辑:现在我已经看到你真正想要的东西,尝试使用地图:

var cleanVehicles = $.map(vehicleText.split("|"), $.trim);

#2


8  

I'm going to suggest not using the overhead of jQuery for a simple for-loop...

我建议不要使用jQuery的开销来进行简单的for循环...

var arVeh = vehicleText.split("|");

for (var i = 0, l = arVeh.length; i < l; ++i) {
    arVeh[i] = $.trim(arVeh[i]);
});

Alternatively, get rid of the whitespace from the beginning, and avoid the need for another loop at all.

或者,从头开始删除空格,并且根本不需要另外的循环。

var arVeh = $.trim(vehicleText).split(/\s*\|\s*/);

#3


1  

var my_arr = ['    cats', 'dogs    ', '  what  '];
$.each(my_arr, function (id, val) {
    my_arr[id] = $.trim(val);
});
console.log(my_arr);

This will trim the value and set it to the indexed item.

这将修剪该值并将其设置为索引项。

#4


1  

You don't have to use JQuery. Here is your vanilla solution:

您不必使用JQuery。这是你的香草解决方案:

testArray.map(Function.prototype.call, String.prototype.trim);

testArray.map(Function.prototype.call,String.prototype.trim);

Function.prototype.call calls trim() on each of the elements of the testArray. As simple as that!

Function.prototype.call在testArray的每个元素上调用trim()。就如此容易!

#5


0  

Could you not just do this?

你能不能这样做吗?

 var arVeh = vehicleText.split("|");

$.each(arVeh, function (idx, val) {

    arVeh[idx] = $.trim(this);

});

#6


0  

Without 'creating' an array in the javascript code (an array will nevertheless be created in memory)

没有在javascript代码中“创建”一个数组(不过将在内存中创建一个数组)

vehicles = $.map(vehicleText.split("|"), function(e,i) { return $.trim(e) });

#7


0  

//a simple function
function trimArray(dirtyArray){
  $.map(dirtyArray.split("|"), function(idx, val){
    return $.trim(this);
  });
}

trimArray(vehicleArray);

should do the trick

应该做的伎俩

Or you could use some of the awesome power of javascript and use array.prototype. I'm still a little new at using the .prototype of any object... so this isnt guaranteed to work (but it certainly can be done).

或者您可以使用javascript的一些强大功能并使用array.prototype。我仍然有点使用任何对象的.prototype ...所以这不能保证工作(但它肯定可以完成)。

Array.prototype.trim = function (){
  $.map(dirtyArray.split("|"), function(idx, val){
    return $.trim(this);
  });
}

someArray.trim()

#8


-1  

You need these two jQuery functions: 1.) iterate through array element with ability to edit items: http://api.jquery.com/jquery.map/

你需要这两个jQuery函数:1。)迭代数组元素,能够编辑项目:http://api.jquery.com/jquery.map/

2.) remove blank spaces from beginning and end of a string: http://api.jquery.com/jQuery.trim/

2.)从字符串的开头和结尾删除空格:http://api.jquery.com/jQuery.trim/

Use them this way:

以这种方式使用它们:

array = $.map(array, function(value) { return value.trim();});

Check this JSFiddle: https://jsfiddle.net/L00eyL4x/49/

检查这个JSFiddle:https://jsfiddle.net/L00eyL4x/49/

#1


50  

You don't even really need the idx or val parameters. This appears to work on jsFiddle:

你甚至不需要idx或val参数。这似乎适用于jsFiddle:

var cleanVehicles = [];

$.each(vehicleText.split("|"), function(){
    cleanVehicles.push($.trim(this));
});

EDIT: Now that I've seen what you're really after, try using map:

编辑:现在我已经看到你真正想要的东西,尝试使用地图:

var cleanVehicles = $.map(vehicleText.split("|"), $.trim);

#2


8  

I'm going to suggest not using the overhead of jQuery for a simple for-loop...

我建议不要使用jQuery的开销来进行简单的for循环...

var arVeh = vehicleText.split("|");

for (var i = 0, l = arVeh.length; i < l; ++i) {
    arVeh[i] = $.trim(arVeh[i]);
});

Alternatively, get rid of the whitespace from the beginning, and avoid the need for another loop at all.

或者,从头开始删除空格,并且根本不需要另外的循环。

var arVeh = $.trim(vehicleText).split(/\s*\|\s*/);

#3


1  

var my_arr = ['    cats', 'dogs    ', '  what  '];
$.each(my_arr, function (id, val) {
    my_arr[id] = $.trim(val);
});
console.log(my_arr);

This will trim the value and set it to the indexed item.

这将修剪该值并将其设置为索引项。

#4


1  

You don't have to use JQuery. Here is your vanilla solution:

您不必使用JQuery。这是你的香草解决方案:

testArray.map(Function.prototype.call, String.prototype.trim);

testArray.map(Function.prototype.call,String.prototype.trim);

Function.prototype.call calls trim() on each of the elements of the testArray. As simple as that!

Function.prototype.call在testArray的每个元素上调用trim()。就如此容易!

#5


0  

Could you not just do this?

你能不能这样做吗?

 var arVeh = vehicleText.split("|");

$.each(arVeh, function (idx, val) {

    arVeh[idx] = $.trim(this);

});

#6


0  

Without 'creating' an array in the javascript code (an array will nevertheless be created in memory)

没有在javascript代码中“创建”一个数组(不过将在内存中创建一个数组)

vehicles = $.map(vehicleText.split("|"), function(e,i) { return $.trim(e) });

#7


0  

//a simple function
function trimArray(dirtyArray){
  $.map(dirtyArray.split("|"), function(idx, val){
    return $.trim(this);
  });
}

trimArray(vehicleArray);

should do the trick

应该做的伎俩

Or you could use some of the awesome power of javascript and use array.prototype. I'm still a little new at using the .prototype of any object... so this isnt guaranteed to work (but it certainly can be done).

或者您可以使用javascript的一些强大功能并使用array.prototype。我仍然有点使用任何对象的.prototype ...所以这不能保证工作(但它肯定可以完成)。

Array.prototype.trim = function (){
  $.map(dirtyArray.split("|"), function(idx, val){
    return $.trim(this);
  });
}

someArray.trim()

#8


-1  

You need these two jQuery functions: 1.) iterate through array element with ability to edit items: http://api.jquery.com/jquery.map/

你需要这两个jQuery函数:1。)迭代数组元素,能够编辑项目:http://api.jquery.com/jquery.map/

2.) remove blank spaces from beginning and end of a string: http://api.jquery.com/jQuery.trim/

2.)从字符串的开头和结尾删除空格:http://api.jquery.com/jQuery.trim/

Use them this way:

以这种方式使用它们:

array = $.map(array, function(value) { return value.trim();});

Check this JSFiddle: https://jsfiddle.net/L00eyL4x/49/

检查这个JSFiddle:https://jsfiddle.net/L00eyL4x/49/