X

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

This post was last modified on July 18, 2011 11:23 am

Categories: Javascript
Tags: Array
Chetan Patel: Chetan Patel is blogger, writes on Tech News, current trends, Gadgets Reviews, Website owner guide and Tips. You can connect him at <a href="https://plus.google.com/118385579556796801405?rel=author">Google+</a> & <a href="https://www.facebook.com/patecj">Facebook</a>.