Hcabnettek Hcabnettek - 2 years ago 114
jQuery Question

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(arVeh, function (idx, val) {



~ck in San Diego

Answer Source

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

var cleanVehicles = [];

$.each(vehicleText.split("|"), function(){

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

var cleanVehicles = $.map(vehicleText.split("|"), $.trim);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download