Trim Array in Javascript: Trim elements of an array in Javascript

If you want to trim elements of an array such that All elements of array should not have space at start or end.

Code:

function trimArray(arr)
{
    for(i=0;i<arr.length;i++)
    {
        arr[i] = arr[i].replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    }
    return arr;
}

Example :

var triotips = new Array("test"," test1","test2 "," test3 ","test4 test5 ");
alert(trimArray(triotips));

Output:
test,test1,test2,test3,test4 test5

If you are looking for trimming Javascript string then see here. Trim Javascript String : Remove space at starting or ending of string

Chetan Patel

Chetan Patel is blogger, writes on Tech News, current trends, Gadgets Reviews, Website owner guide and Tips. You can connect him at Google+ & Facebook.