Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an object is not an array?

So i have a function that needs to check if an argument is an object, but this fails because:

typeof [] // returns 'object' 

This is a classic javascript gotcha, but i cant remember what to do to actually accept objects, but not arrays.

like image 782
hojberg Avatar asked Feb 15 '10 12:02

hojberg


People also ask

How can you tell if an object is an array or an array?

The first approach would be to find the array index of the search object using Array. findIndex(). Once the search index is found, we can access the search object by “array[index]” and then perform any required operations on the object that is found.

How do you know if it is an array?

The isArray() method returns true if an object is an array, otherwise false .

How do you check if an array has an object?

Using includes() Method: If array contains an object/element can be determined by using includes() method. This method returns true if the array contains the object/element else return false. Example: html.

How do you check if an object is an array in react?

To check if an element exists in an array in React: Use the includes() method to check if a primitive exists in an array. Use the some() method to check if an object exists in an array.


2 Answers

Try something like this :

obj.constructor.toString().indexOf("Array") != -1 

or (even better)

obj instanceof Array 
like image 101
Tomas Vana Avatar answered Sep 18 '22 23:09

Tomas Vana


All these answers suggesting that you check to see (one way or another) if an object is an instance of the "Array" class (that is, constructed by "Array") are really not safe solutions. They'll work sometimes, maybe most of the time, but all the major frameworks have moved away from that approach. One of the main problems with it comes about when there's interaction between multiple windows (generally, a parent window and one or more frame or iframe windows). If you pass an array object created in one window into an API resident in another window, all these tests will fail. Why? Because what you're testing is whether an object is an instance of the "Array" class in your local window context. In other words, when you reference "Array" in

if (myobject instanceof Array) { ... } 

what you're referencing is window.Array, of course. Well, an array constructed in another window is not going to be an instance of the Array class in your window!

Checking the constructor name is probably a little safer, though it's still risky. In my opinion, you're better off taking a duck-typing approach. That is, instead of asking, "Is this an Array?" ask instead, "does this object seem to support some particular set of Array APIs I need in this circumstance?" For example, "does this object have a length property?" Javascript is a pretty "soft" language, and just about everything's mutable. Thus even if you do find out something was constructed by "Array", you still really don't know for sure what you can do with it or to it.

[edit] Thanks for that link, @Lachlan - here's a very clear description of the issues: http://juhukinners.com/2009/01/11/typeof-considered-useless-or-how-to-write-robust-type-checks/

like image 24
Pointy Avatar answered Sep 21 '22 23:09

Pointy