Results 1 to 12 of 12

Thread: Javascript: test if an object is an array.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    27

    Javascript: test if an object is an array.

    Hi! I would like to know if there is an elegant way to test if an object is an array.

    Example:
    Code:
    function ProcessObject(object) {
      //Test if it is an array.
      if(...) {
        for(int i=0; i < object.length; i++) {
          //object[i] ...
        }
      }
      else {
        //Don't process as an array.
        //object.value  ...
      }
    }
    In the code above, the function is receiving an object that could be an array or not. The main reason I want to know if it is an array is because I want to use the length property. Do anyone know if there is a property in DOM to know if an object is an array so I can invoke the length property without error?

    Thanks a lot!

    PS: I make it, in a not elegant way...
    Last edited by Tribo; Mar 25th, 2008 at 03:56 PM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Javascript: test if an object is an array.

    Code:
    if(object.length != null)
    {
    //Array!
    }
    I think...

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    27

    Re: Javascript: test if an object is an array.

    Quote Originally Posted by mendhak
    Code:
    if(object.length != null)
    {
    //Array!
    }
    I think...
    Perfect! It was so simple, and I was looking for a complicated solution...

    Thanks a lot!!!

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Javascript: test if an object is an array.

    We can provide you with an overly complicated solution too, if that is what you wish.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Javascript: test if an object is an array.

    But strings have a length property, too. Are they arrays? Sort of, but probably not the type you mean.

    Code:
    if (object instanceof Array)
      // do something

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    27

    Re: Javascript: test if an object is an array.

    Quote Originally Posted by penagate
    But strings have a length property, too. Are they arrays? Sort of, but probably not the type you mean.

    Code:
    if (object instanceof Array)
      // do something
    I don't remember now, but I think that they are an array of DIVs, and with JavaScript I change the style so I can hide the information or not (it is a kind of Detail that the user could see and by default it isn't shown)

    Regards!

  7. #7
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Javascript: test if an object is an array.

    This is brute but at least it does the job. Tested it too.

    Code:
    		if(obj[0] != null){
    			alert("array of length " + obj.length);
    		} else {
    			alert("not an array");
    		}

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Javascript: test if an object is an array.

    What's wrong with instanceof? Does it smell funny or something?

  9. #9
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Javascript: test if an object is an array.

    Sorry, posting code for future reference on event listeners. eace:


    Code:
    <html>
    <head>
    <title>Test Array</title>
    <meta http-equiv="Content-Type" content="text/html; charset=shift_jis">
    <script type="text/javascript">
    
    	var mydivcount = 0;
    	
    	function checkIfArray(obj){	
    	
    		if(obj[0] != null){
    			alert("array of length " + obj.length);
    		} else {
    			alert("not an array");
    		}
    	}	
    	
    	function addDiv(){	
    		var newdiv = document.createElement('div');
    		newdiv.setAttribute('id', 'mydiv');		
    		newdiv.setAttribute('name', 'mydiv');						
    		
                    // dynamic event listener
    		if (newdiv.addEventListener){
    		  newdiv.addEventListener('click', removeDiv, false); 
    		} else if (newdiv.attachEvent){
    		  newdiv.attachEvent('onclick', removeDiv);
    		}
    		
    		newdiv.appendChild(document.createTextNode("This is another div: " + (++mydivcount)))
    		document.getElementById('addhere').appendChild(newdiv);
    	}
    	
    	function removeDiv(evt){
    		// target for Mozilla
    		// srcElement for IE
    		var el = (evt.target) ? evt.target : evt.srcElement;
    		document.getElementById('addhere').removeChild(el);
    	}
    
    </script>
    </head>
    
    <body>
    <div id="addhere" style="border:1 solid #000000; padding: 10px 0px 10px 0px ">
    </div>
    
    <input type="button" value="Add a DIV" onClick="addDiv()">
    <input type="button" value="Check Array" onClick="checkIfArray(document.getElementsByName('mydiv'))">
    
    </body>
    </html>

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Posts
    27

    Re: Javascript: test if an object is an array.

    Quote Originally Posted by penagate
    What's wrong with instanceof? Does it smell funny or something?
    No, I think that it is the most elegant solution (it makes the code more legible). I'm going to try it.

    Regards,

  11. #11
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Javascript: test if an object is an array.

    Quote Originally Posted by penagate
    What's wrong with instanceof? Does it smell funny or something?

    I tried instanceof but if the collection is other than an Array type like divs for instance it won't detect it. the other testing is versatile enough I believe.

    Code:
                    if(obj instanceof Array){
    			alert("array of length " + obj.length);
    		} else {
    			alert("not an array");
    		}

  12. #12
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Javascript: test if an object is an array.

    DOM node collections are not arrays. In fact there are 'live' collections as well as 'non-live' ones and fragments and so forth. None of these are derived from Array.

Posting Permissions

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



Click Here to Expand Forum to Full Width