SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to detect an array...

    Pardon a silly question, but what's the best way to determine if a variable is an array in JavaScript? I need to treat it differently depending of if it's a string or an array. Thanks in advance.
    Nate Baldwin
    mindpalette.com

  2. #2
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    use varaiable.length

    Quote Originally Posted by Nate Baldwin
    Pardon a silly question, but what's the best way to determine if a variable is an array in JavaScript? I need to treat it differently depending of if it's a string or an array. Thanks in advance.
    you can use

    if(variable.length>1) {............} else {..............}

    if it still says 1, and their are 2 or more numbers split with a ','
    then the array might be contained in an array,. in which case you will need to split it.
    ie
    array="'1,2,3,4,5','6,7,8,9,0'"
    so
    array length = array.length = 2
    first array = array[0]; =1,2,3,4,5
    second array = array[1] =6,7,8,9,0

    first array length = (array[0].split(",")).length = 5
    second array length =(array[1].split(",")).length = 5

    whole length
    = (array[0].split(",")).length+(array[1].split(",")).length

    or

    whole length = ((array[0]+","+array[1]).split(",")).length

    hope it helps
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  3. #3
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This will work
    Code:
    function isArray( variable )
    {
    	return Boolean( typeof variable == 'object' && variable.length >= 0 );
    }
    Checking for the length property only will also identify Strings. Here's the test page I made to corroborate my solution
    Code:
    var int = 9;
    var str = 'hello';
    var arr = [1,2,3,4];
    var ar2 = new Array();
    var obj = {'one':1,'two':2}
    var ob2 = new Object();
    var fnc = new Function( "alert('hi')" );
    Code:
     
    function isArray( variable )
    {
    	return Boolean( typeof variable == 'object' && variable.length >= 0 );
    }
     
    document.writeln( "Integer: " + isArray( int ) + "<br>" );
    document.writeln( "String: " + isArray( str ) + "<br>" );
    document.writeln( "Array: " + isArray( arr ) + "<br>" );
    document.writeln( "Array: " + isArray( ar2 ) + "<br>" );
    document.writeln( "Object: " + isArray( obj ) + "<br>" );
    document.writeln( "Object: " + isArray( ob2 ) + "<br>" );
    document.writeln( "Function: " + isArray( fnc ) + "<br>" );
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •