Several ways to determine whether an object is an array in JavaScript (summary)

青灯夜游
Release: 2021-04-13 09:35:01
forward
3418 people have browsed it

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.

Several ways to determine whether an object is an array in JavaScript (summary)

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 thetypeofoperator 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
Copy after login

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]

Use the Array.isArray() method

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, theArray.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
Copy after login

Use the object's constructor attribute

Every object has aconstructorproperty (except for objects created usingobject.create(null), which is unlikely to occur). We can directly compare theconstructorattribute 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 thenewkeyword, the constructor is used. For example, inlet myArray = new Array(1,2), the constructor used isArray().

You can use theconstructorattribute 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
Copy after login

Use the instanceof operator

instanceof Theoperator checks whether the constructor is found in the object's prototype chain.

Like thetypeofoperator, it returns a boolean value. To determine if a variable is an array, you can useinstanceofas 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
Copy after login

Use the Object.prototype.call() method

JS All objects in inherit properties from the main prototype object, which is namedObject.prototype. ThetoString()method exists inObject.prototype. This is the reason why each object has its owntoString()method.Object.prototypeThetoString()method displays the type of object.

Thecall()method of an object executes a function but changes thethisvalue to the object passed in as argument, e.g. it allows one object to use another object Methods.

So, we can useObject.prototype.toString()to print the type, then usecall()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
Copy after login

We are unlikely to use this method, but it never hurts to learn more about JS objects

Summary

In this article , we looked at several ways in JS to determine whether an object is an array. The simplest method is theArray.isArray()method, and most friends may use it in the future.

However, we can also utilize theinstanceofoperator 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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!