开发者

jQuery .each help, I want to trim() all the strings in an array

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.

var arVeh = vehicleText.split("|");
    var cleanArry = new Array();
    $.each(ar开发者_运维问答Veh, function (idx, val) {

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

    });

Cheers, ~ck in San Diego


You don't even really need the idx or val parameters. This appears to work on 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);


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

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


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

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


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.


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

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!


Could you not just do this?

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

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

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

});


//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).

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

someArray.trim()


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

2.) remove blank spaces from beginning and end of a string: 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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜