无论如何要知道特定类型的JavaScript变量

这个问题在这里已经有了答案:

  • 检查对象是否是数组? 38个答案

  • Array.isArray(data)将返回true,因为数据是一个数组。

    编辑:

    在这个答案的评论中澄清之后, Array.isArray(data[0])将检查数组数据中的第一个元素是否是数组。


    你可以使用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/27579.html

    上一篇: Is there anyway to know specific type of javascript variable

    下一篇: A more refined Javascript typeof?