• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5395
  • Last Modified:

isarray

Is it possible to detect if a variable is an array without using the length attribute or without using [0] accessors? I dont want to get a warning or error when doing this.
0
jstretch
Asked:
jstretch
  • 2
2 Solutions
 
BogoJokerCommented:
Hi jstretch,

Here is a solution, somebody had the same isArray() problem and wrote their own function:
http://www.planetpdf.com/developer/article.asp?ContentID=6383
I'll try and post an example of it.

Joe P
0
 
nabsolCommented:
Hi

This is the code for checking array.
   var a = new Array()
  if(a.constructor.toString().indexOf("Array") != -1)
      alert("array")

By Nab
0
 
BogoJokerCommented:
Here is some sample code, the isArray() method is Kas Thomas's creation and solution for checking if an object is an array.  Just pass the method your object, it checks arguments[0] so if you pass nothing it does nothing, if you pass more then one object it will only check the first object.

[javascript code]
<script type="text/javascript">
function isArray()
{
  if (typeof arguments[0] == 'object')
  {  
    var criterion = arguments[0].constructor.toString().match(/array/i);
   return (criterion != null);  
  }
  return false;
}

var array = new Array('Joe');
alert(array.constructor.toString());
var str = "Joe2";
alert(str.constructor.toString());
if (isArray(array))
  alert("yes");
else
  alert("no");
if (isArray(str))
  alert("yes");
else
  alert("no");
</script>

I put a bunch of alerts() so I could see what is going on and so I can explain to you what is happening.  First, look at the code outside of the function.  It creates an array and a string then uses the isArray() function (which returns true/false) and prints yes if it is an array or no if it is not.  Obviously this will do "yes" then "no".

I learned that when you create an object it shows the constructor used to initiate the object if you do (objectName).constructor.toString().  I learned that from his isArray() method which gets that string and in this case looks for the word Array.  Notice that when I create the Array I print the constructor, it indeed has Array, and the string constructor has String, NOT Array.  So that is simple enough.  Now for his isArray() function!

The isArray() does take params.  Not the normal way so don't be fooled.  It checks arguments[]s array, onlyt he first position [0].  Now comes the logic.  It first checks if the param is an object.  Fair enough, if you passed no argument it would be null and we would be done, it would then return false.  If you pass a param it will then check the constructor (as a string using toString()) and search using a regular expression (case insensitive) for the word array.  If found, it again makes sure the array is not actually null and returns true (or false if it was null).  Pretty brillient solution and I learned some intresting stuff.

Hope this works for you,
Joe P
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now