Javascript jQuery .each 帮助,我想修剪()数组中的所有字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4411147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery .each help, I want to trim() all the strings in an array
提问by Hcabnettek
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
干杯,~在圣地亚哥
回答by Chris Doggett
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);
回答by nickf
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*/);
回答by John Giotta
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.
这将修剪该值并将其设置为索引项。
回答by Satya Sampathirao
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()。就如此容易!
回答by Jerome WAGNER
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) });
回答by Derek Adair
//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()
回答by Jerryf
Could you not just do this?
你不能只做这个吗?
var arVeh = vehicleText.split("|");
$.each(arVeh, function (idx, val) {
arVeh[idx] = $.trim(this);
});
回答by rogerwamba
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/