Home > Article > Web Front-end > Several ways to determine whether an object is an array in JavaScript (summary)
This article will share with you several JavaScript methods to determine whether an object is an array. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Using arrays in JS is a common operation. Sometimes during development, we get a variable that needs to be an array, but we are not sure whether it is an array. What should we do? How to determine whether it is an array?
Non-primitive data types in JS are objects (functions have their own types, but they are also objects). Therefore, just using the typeof
operator to determine is not enough:
let result = { subject: 'Science', marks: 97 }; let numbers = [1, 2, 3, 4, 5]; console.log(typeof result); // Object console.log(typeof numbers); // Object
In this article, let’s look at how to check if a given variable or value is an array in JS. [Related tutorial recommendations: JavaScript Video Tutorial]
As the name suggests, this method can be used to identify given parameters Whether it is an array, it returns a boolean value (true/false
) and the result.
For example, using the following variables, the Array.isArray()
method can correctly determine whether it is an array:
let result = { subject: "Science", marks: 97 }; // Object let numbers = [1, 2, 3, 4, 5]; // Array let name = "Mark"; // String let names = new Array("Jill", "Jane", "Jacqueline"); console.log(Array.isArray(result)); // false console.log(Array.isArray(numbers)); // true console.log(Array.isArray(name)); // false console.log(Array.isArray(names)); // true
Every object has a constructor
property (except for objects created using object.create(null)
, which is unlikely to occur). We can directly compare the constructor
attribute with the JS constructor. So if we compare it to the array constructor, we'll know if it's an array.
Note: The constructor is a function used to initialize an object. If an object is created using the new
keyword, the constructor is used. For example, in let myArray = new Array(1,2)
, the constructor used is Array()
.
You can use the constructor
attribute to determine whether a variable is an array:
let result = { subject: "Science", marks: 97 }; let numbers = [1, 2, 3, 4, 5]; let name = "Mark"; let names = new Array("小智", "小力", "小吴"); console.log(result.constructor === Array); // false console.log(numbers.constructor === Array); // true console.log(name.constructor === Array); // false console.log(names.constructor === Array); // true
instanceof The
operator checks whether the constructor is found in the object's prototype chain.
Like the typeof
operator, it returns a boolean value. To determine if a variable is an array, you can use instanceof
as follows:
let result = { subject: "Science", marks: 97 }; let numbers = [1, 2, 3, 4, 5]; let name = "Mark"; let names = new Array("小智", "小力", "小吴"); console.log(result instanceof Array); // false console.log(numbers instanceof Array); // true console.log(name instanceof Array); // false console.log(names instanceof Array); // true
JS All objects in inherit properties from the main prototype object, which is named Object.prototype
. The toString()
method exists in Object.prototype
. This is the reason why each object has its own toString()
method. Object.prototype
The toString()
method displays the type of object.
The call()
method of an object executes a function but changes the this
value to the object passed in as argument, e.g. it allows one object to use another object Methods.
So, we can use Object.prototype.toString()
to print the type, then use call()
to process another object, and then compare the string value to determine whether it is an array.
let result = { subject: "Science", marks: 97 }; let numbers = [1, 2, 3, 4, 5]; let name = "Mark"; let names = new Array("小智", "小力", "小吴"); console.log(Object.prototype.toString.call(result)); // [object Object] console.log(Object.prototype.toString.call(numbers)); // [object Array] console.log(Object.prototype.toString.call(name)); // [object String] console.log(Object.prototype.toString.call(names)); // [object Array] console.log(Object.prototype.toString.call(result) === "[object Array]"); // false console.log(Object.prototype.toString.call(numbers) === "[object Array]"); // true console.log(Object.prototype.toString.call(name) === "[object Array]"); // false console.log(Object.prototype.toString.call(names) === "[object Array]"); // true
We are unlikely to use this method, but it never hurts to learn more about JS objects
In this article , we looked at several ways in JS to determine whether an object is an array. The simplest method is the Array.isArray()
method, and most friends may use it in the future.
However, we can also utilize the instanceof
operator and other object properties to determine whether it is an array.
Original address: https://stackabuse.com/javascript-check-if-object-is-array/
Author: Guest Contributor
Translation address :https://segmentfault.com/a/1190000038661505
For more programming-related knowledge, please visit: Programming Video! !
The above is the detailed content of Several ways to determine whether an object is an array in JavaScript (summary). For more information, please follow other related articles on the PHP Chinese website!