How To Determine If Parameter Is An Array Or An Object In Javascript?
I have a prototype like this; LocalDataEngine.prototype.ExecuteNonQuery = function (query) { } And I call this prototype within two different argument like below; By using object
Solution 1:
You can use instanceof
:
% js
> [] instanceofArraytrue
> {} instanceofArrayfalse
It will work flawlessly if you are not using frames (which is probably a bad idea anyway). If you are using frames and ECMAScript 5, use Array.isArray
:
> Array.isArray({})
false
> Array.isArray([])
true
See the duplicate question linked by thg435 for additional solutions.
Solution 2:
Like this:
if (query instanceofArray) {
return'array';
} elseif (query instanceofObject) {
return'object';
} else {
return'scalar';
}
Solution 3:
if( Object.prototype.toString.call( yourObj) === '[object Array]' ) {
alert( 'Array!' );
}
Post a Comment for "How To Determine If Parameter Is An Array Or An Object In Javascript?"