关于数组:是否仍要知道特定类型的javascript变量

Is there anyway to know specific type of javascript variable

本问题已经有最佳答案,请猛点这里访问。

我的变量data如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var data = [
{
    id: 1,
    name:"John",
    sex:"M",
    maritalStatus:"M",
    dob:"01-01-1990",
    title:"Software Engineer",
    address:"VN",
    phoneNumber:"(123) 456-7890",
    email:"[email protected]"
}
];

var data = [
    ["1","John","M","M","1990","Software Engineer","[email protected]","(123) 456-7890"],
];

有没有办法知道他们的类型。我用的是typeof(data),但都表明这是对象。


可以使用Objist.Trimest.toStRew()调用(对象)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var data =
{
 id: 1,
 name:"John",
 sex:"M",
 maritalStatus:"M",
 dob:"01-01-1990",
 title:"Software Engineer",
 address:"VN",
 phoneNumber:"(123) 456-7890",
 email:"[email protected]"
};

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:"[email protected]"
}
];


console.log(Object.prototype.toString.call(data2) ); //[object Array]

var data1 = [
               ["1","John","M","M","1990","Software Engineer","[email protected]","(123) 456-7890"],
            ];

console.log(Object.prototype.toString.call(data1) );//[object Array]

由于数据是一个数组,因此Array.isArray(data)将返回true。

编辑:

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