Sometimes I find that I need to validate if a variable has an array or object as its structure, in such a way that it can take a corresponding action depending on the type of value stored in the variable.
What would be recommended to distinguish between an array and an object?
Speaking of an object that is not the product of an instantiated function.
if (isObject(valor)) {
/* Código */
} else {
/* Código */
}
In JavaScript (ECMAScript 5) there are not only objects and arrays, there are a total of 5 primitive types:
string, number, undefined, boolean, object
The usual way is to use the operator
typeof
that will return the corresponding string, that is:How? Yes, arrays are regular objects (where
typeof [] === "object"
) but they have a relationship between thekey
(notation:valor[key]
) and the propertylength
. They also inherit fromArray.prototype
.A good way (supported by all current browsers) to check if an object is an array is:
Array.isArray(valor)
So you can't just tell the difference between array and object, since there's a chance that it's neither one thing nor the other.
You could do the following:
But beware! , there is an exception,
typeof null === 'object'
so you should also check if the value is null or not, because a variable with valuenull
would give a false positive as an object. This is a bug in ECMAScript 5, version 6 fixes it and returns 'null'.I want to clarify that the variable could contain a function in which case it would be
typeof function(){} === 'function'
More info on MDN , unfortunately it's in English.
You can use
Object.prototype.toString.call(valor)
to know the object type ofvalor
A simple method would be to apply the function
JSON.stringify()
to the variable and check what the first character of the resulting string is:[
), the variable was an array.{
), the variable was an object.With that in mind, I've created a simple function that returns the string "array" if the parameter was an array, "object" if the parameter was an object, or "other" if it's another type of variable:
Here is a series of examples:
The best way to validate that an object is of the JSON or Array type, I recommend the following, taking into account that:
Solution 1
Solution 2
But strictly speaking, an array is part of the JSON syntax. That said, the following two examples are a valid part of that structure.
Example 1:
Example 2:
In case of having a variable of type text (String) that you want to validate its structure to know if it coincides with any of both types mentioned, it would be advisable to use the following function:
You could use to detect json a
You can see more info about the typeof here
Some time ago I had a similar problem, and I found the following solution, where u
type
is assigned depending on what type your is , in this example it is an object.array
object
json
I hope it works for you.
Short answer
If your target is modern browsers, use
Array.isArray(variable)
the one introduced in ECMAScript 5. All details in Array.isArray() .Explanation
According to ECMAScript 7 , an array, also called an array, is an exotic object that gives special treatment to array/array index property keys.
In other words, a shared array is an object with certain peculiarities which made it difficult to distinguish between an array and an object that is not an array. Fortunately
Array.isArray()
it was introduced as a standard feature and is supported by modern browsers.Demonstration
However, if you use a more general solution, where the data type is identified, consider using
toString.call()
it as this will return a string of the form [object class ] where class is indicates the data type.Note: Do not assume that because object is indicated for primitives, everything is an object in JavaScript as this is incorrect. What happens is that JavaScript temporarily converts the primitive to an object in order to be able to call properties of the corresponding object, in this case the function
toString