Is there anyway to know specific type of javascript variable

This question already has an answer here:

  • Check if object is array? 38 answers

  • Array.isArray(data) will return true since data is an array.

    EDIT:

    After clarification in the comments of this answer, Array.isArray(data[0]) will check if the first element in the array data is an array.


    你可以使用Object.prototype.toString()。call(object)

    var data = 
    {
     id: 1,
     name: "John",
     sex: "M",
     maritalStatus: "M",
     dob:"01-01-1990",
     title:"Software Engineer",
     address:"VN",
     phoneNumber:"(123) 456-7890",
     email: "john@gmail.com"
    };
    
    console.log(Object.prototype.toString.call(data) ); //[object Object]
    
    
    var data2 = [
    {
     id: 1,
     name: "John",
     sex: "M",
     maritalStatus: "M",
     dob:"01-01-1990",
     title:"Software Engineer",
     address:"VN",
     phoneNumber:"(123) 456-7890",
     email: "john@gmail.com"
    }
    ];
    
    
    console.log(Object.prototype.toString.call(data2) ); //[object Array]
    
    var data1 = [
                   ["1", "John", "M", "M", "1990", "Software Engineer", "john@gmail.com", "(123) 456-7890"],
                ];
    
    console.log(Object.prototype.toString.call(data1) );//[object Array]
    
    链接地址: http://www.djcxy.com/p/27580.html

    上一篇: 类和数组真的只是JavaScript中的一个对象吗?

    下一篇: 无论如何要知道特定类型的JavaScript变量