Home  >  Article  >  Daily Programming  >  How to determine whether an object is an array in JS

How to determine whether an object is an array in JS

藏色散人
藏色散人Original
2018-12-13 14:24:2420028browse


Use JS to determine whether the object is an array. You can use the JavaScript Array.isArray() method to check whether the object (or variable) is an array. If the return value is true, it is represented as an array; otherwise, false is returned.

How to determine whether an object is an array in JS

Below we will combine specific code examples to introduce to you how to use js to determine whether an object is an array.

First we create some variables to check whether these variables are objects.

The code example is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
</body>
<script>
    // 创建一些变量
    var v1 = {name: "John", age: 18};
    var v2 = ["red", "green", "blue", "yellow"];
    var v3 = [1, 2, 3, 4, 5];
    var v4 = null;

    // 测试变量数据类型
    document.write(typeof(v1) + "<br>"); 
    document.write(typeof(v2) + "<br>"); 
    document.write(typeof(v3) + "<br>"); 
    document.write(typeof(v3) + "<br>"); 
</html>

Here we use typeof() to detect the data types of the above four variables.

The result is as follows:

How to determine whether an object is an array in JS

As shown in the figure, v1, v2, v3, and v4 are all object types.

So how do we determine whether these objects are arrays?

The code example is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
</body>
<script>
    // 创建一些变量
    var v1 = {name: "John", age: 18};
    var v2 = ["red", "green", "blue", "yellow"];
    var v3 = [1, 2, 3, 4, 5];
    var v4 = null;
    // 测试变量是否为数组
    document.write(Array.isArray(v1) + "<br>");  
    document.write(Array.isArray(v2) + "<br>");  
    document.write(Array.isArray(v3) + "<br>");  
    document.write(Array.isArray(v4) + "<br>");  
</script>
</html>

JavaScript isArray() method. The isArray() method is used to determine whether an object is an array. Returns true if the object is an array, false otherwise.

The judgment results are as follows:

How to determine whether an object is an array in JS

As shown in the figure, objects v2 and v3 are arrays.

Note: All mainstream browsers support the Array.isArray() method, such as Chrome, Firefox, IE (version 9 and above), etc.

This article is a detailed introduction to JS to determine whether an object is an array. It is also very simple and easy to understand. I hope it will be helpful to friends in need!


The above is the detailed content of How to determine whether an object is an array in JS. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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