Example of using JS to determine whether an element is an array

Example of using JS to determine whether an element is an array

Here are the types of data that can be verified

let a = [1,2,3,4,5,6];
 let b = [
 {name: 'Zhang Fei', type: 'tank'},
 {name: 'Guan Yu', type: 'soldier'},
 {name: 'Liu Bei', type: 'shooter'},
 ];
 let c = 123;
 let d = 'www';
 let e = {name: 'Angela', type: 'mage'};

1. Through Array.isArray()

Array.isArray() can determine whether an element is an array, if so, it returns true, otherwise it returns false

console.log(Array.isArray(a)); // true
 console.log(Array.isArray(b)); // true
 console.log(Array.isArray(c)); // false
 console.log(Array.isArray(d)); // false
 console.log(Array.isArray(e)); // false

2. Judge by instanceof

The instanceof operator is used to detect whether an instance belongs to an object's prototype chain.

console.log(a instanceof Array); // true
 console.log(b instanceof Array); // true
 console.log(c instanceof Array); // false
 console.log(d instanceof Array); // false
 console.log(e instanceof Array); // false

It can also be used to judge objects

console.log(e instanceof Object); // true

To determine whether it is an array, we need to check whether the Arrray.prototype property exists in the prototype chain of the variable array (a, b). Obviously, a and b are arrays and have the Arrray.prototype property, so it is true.

3. Judge by the constructor of the object constructor

Each instance of Object has a constructor that stores the function that creates each object.

console.log(a.constructor === Array); // true
console.log(b.constructor === Array); // true

The following includes other data type validations

console.log(c.constructor === Number); // true
console.log(d.constructor === String); // true
console.log(e.constructor === Object); // true

4. Judge by Object.prototype.toString.call()

Look up the call through the prototype chain

console.log(Object.prototype.toString.call(a) === '[object Array]'); // true
console.log(Object.prototype.toString.call(b) === '[object Array]'); // true

The following includes other data type validations

console.log(Object.prototype.toString.call(c) === '[object Number]'); // true
console.log(Object.prototype.toString.call(d) === '[object String]'); // true
console.log(Object.prototype.toString.call(e) === '[object Object]'); // true

5. Judge by isPrototypeOf() on the object prototype chain

The Array.prototype property is the prototype of the Array constructor, which contains a method isPrototypeOf() that is used to test whether an object exists in the prototype chain of another object.

console.log(Array.prototype.isPrototypeOf(a)); // true
 console.log(Array.prototype.isPrototypeOf(b)); // true
 console.log(Array.prototype.isPrototypeOf(c)); // false
 console.log(Array.prototype.isPrototypeOf(d)); // false
 console.log(Array.prototype.isPrototypeOf(e)); // false

Summarize

This is the end of this article about using JS to determine whether an element is an array. For more relevant JS judgment elements are array content, please search 123WORDPRESS.COM previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to determine whether an array contains a specified element in JavaScript
  • JS implementation code to determine whether an element is in an array
  • Summary of JS's method for determining whether there are duplicate elements in an array
  • JavaScript function to determine whether an array already contains a certain element
  • JS implementation example of judging whether an array contains a certain element
  • js function implementation method to determine whether an array contains an element
  • Write a js extension method to determine whether an array contains an element
  • A collection of methods for determining whether elements in an array are repeated in javascript
  • Use js to determine whether an array contains a certain element (similar to in_array() in php)
  • Summary of JS methods for determining whether an array contains an element

<<:  Research on the effect of page sidebar realized by JS

>>:  Various types of jQuery web page verification code plug-in code examples

Recommend

A detailed introduction to HTML page loading and parsing process

The order in which the browser loads and renders H...

Best Practices for Developing Amap Applications with Vue

Table of contents Preface Asynchronous loading Pa...

Detailed explanation of commands to view linux files

How to view linux files Command to view file cont...

Complete steps to build a Laravel development environment using Docker

Preface In this article, we will use Docker to bu...

Detailed explanation of BOM and DOM in JavaScript

Table of contents BOM (Browser Object Model) 1. W...

Tutorial on deploying jdk and tomcat on centos7 without interface

1. Install xshell6 2. Create a server connection ...

Vue codemirror realizes the effect of online code compiler

Preface If we want to achieve the effect of onlin...

How to use Zen coding in Dreamweaver

After I published my last article “Zen Coding: A Q...

CSS to achieve horizontal lines on both sides of the middle text

1. The vertical-align property achieves the follo...

Service management of source package installation under Linux

Table of contents 1. Startup management of source...

Example analysis to fix problems in historical Linux images

Fix for issues with historical Linux images The E...

Detailed explanation of Linux redirection usage

I believe that everyone needs to copy and paste d...

File sharing between Ubuntu and Windows under VMware

This article records the method of sharing files ...

Introduction to Spark and comparison with Hadoop

Table of contents 1. Spark vs. Hadoop 1.1 Disadva...

Vue implements a small countdown function

Countdown function needs to be implemented in man...