0

Is there a good built-in way that I can find whether value is a array or not?

One simple check I can think of is as follows, but I don't like it:

if(ele.push){ /* its array it has push method */ }

I mean i would like know if something like pseudo-code below exists. typeof doesn't seems to be applicable, as it only returns "object" (though that makes sense).

function x(ele){ if(isArray(ele)){ /* dosomething() */ } }
Oleg V. Volkov
  • 21,719
  • 4
  • 44
  • 68
Anil Namde
  • 6,452
  • 11
  • 63
  • 100
  • possible duplicate of [How to detect if a variable is an array](http://stackoverflow.com/questions/1058427/how-to-detect-if-a-variable-is-an-array) – kennytm May 21 '10 at 16:17

3 Answers3

2

http://www.andrewpeace.com/javascript-is-array.html

<script type="text/javascript">
  function is_array(input){
    return typeof(input)=='object'&&(input instanceof Array);
  }
</script>
Pepper
  • 2,932
  • 4
  • 25
  • 26
0
element.constructor == Array
Matchu
  • 83,922
  • 18
  • 153
  • 160
0

Not the cleanest but...

function isArray(obj) {
    return obj.constructor == Array;
}
dreadwail
  • 15,098
  • 21
  • 65
  • 96