JavaScript checks whether an object is an array,
This article shares with you three JavaScript methods to determine whether an object is an array,
1. typeof
The first thing we will think of is to use typeof to detect data types. However, for basic types such as Function, String, Number, and Undefined, typeof can be used to detect data types, for example, the Code is as follows:
function test(){}console.log(typeof 1); // numberconsole.log(typeof test); // function console.log(typeof "yunxi"); // stringconsole.log(typeof undefined); // undefined
But for arrays or regular expressions, if typeof is used for detection, it cannot be satisfied, because when we detect arrays or regular expressions, the returned type will be an object, the following code is used:
console.log(typeof []); // objectconsole.log(typeof /\d+/g); // object
2. Instanceof
Therefore, we can easily think of using instanceof to check whether an object is an array instance. This check returns a boolean value. If it is an array, true is returned, otherwise, false is returned. Let's take a look at the code for checking whether the array is as follows:
console.log([] instanceof Array); // trueconsole.log(/\d+/g instanceof Array); // false
As shown above, instanceof can be used to determine whether it is an array column;
3. constructor attributes
In javascript, each object has a constructor attribute, which references the constructor that initializes the object. For example, to determine the type of an unknown object, we can write a method as follows:
Function isArray (obj) {return typeof obj = 'object' & obj. constructor = Array} // test democonsole. log (isArray ([]); // truevar a = {"a": 1}; console. log (isArray (a); // falsevar B = [1, 2, 3]; console. log (isArray (B); // trueconsole. log (isArray (/\ d +/g); // false
As shown above, you can call the isArray method to determine whether it is an array column.
We can now see that the instanceof method and the constructor attribute can both be used to determine whether it is an array, but there are also external columns, for example, when using an array on the page during cross-frame iframe, it will fail, because in Different frames iframe, the created array will not share its prototype attribute with each other; the following code can be verified after testing ~
Var iframe = document. createElement ('iframe'); document. body. appendChild (iframe); xArray = window. frames [window. frames. length-1]. array; var arr = new xArray ("1", "2", "3", "4", "5"); // This method is not supported in IE, the standard browser firefox and chrome has a console. log (arr); // print the ["1", "2", "3", "4", "5"] console. log (arr instanceof Array); // false console. log (arr. constructor === Array); // false
None of the above methods can determine whether an object is an array. However, we can see in ECMA262 that we can useObject. prototype. toString. call () methodTo determine whether an object is an array. The following code:
Function isArray (obj) {return Object. prototype. toString. call (obj) = '[object Array]';} // The Code calls the console. log (isArray ([]); // trueconsole. log (isArray ([1, 2, 3]); // truevar iframe = document. createElement ('iframe'); document. body. appendChild (iframe); xArray = window. frames [window. frames. length-1]. array; var arr = new xArray ("1", "2", "3", "4", "5"); console. log (arr); // ["1", "2", "3", "4", "5"] console. log (isArray (arr); // true
The above is all the content in this article. It helps you to learn how to determine whether an object is an array in JavaScript, and I hope it will be helpful for your learning.
Articles you may be interested in:
- Javascript checks whether a variable is an array or an object (array or object)
- Js implements array deduplication to determine whether the array and object content are the same
- Js syntax learning: determining whether an object is an array
- JavaScript method for determining whether a variable is an object or an array
- Methods In JavaScript to determine whether a variable is an array, function, or object type