在JavaScript中如何检测天气类型是Array还是Object?
In JavaScript how to detect weather the type is Array or Object?
我需要知道如何检查变量是数组还是对象
var arr = ['foo', 'bar'];
var obj = {
0: 'foo',
1: 'bar'
}
document.write('arr is an: ' + typeof arr + ', obj is an: ' + typeof obj)
// The result is always:
// arr is an: object, obj is an: object
有什么方法可以区分这两种类型?
Array.isArray(arr)
将 return true
。
Array.isArray(obj)
将 return false
.
我需要知道如何检查变量是数组还是对象
var arr = ['foo', 'bar'];
var obj = {
0: 'foo',
1: 'bar'
}
document.write('arr is an: ' + typeof arr + ', obj is an: ' + typeof obj)
// The result is always:
// arr is an: object, obj is an: object
有什么方法可以区分这两种类型?
Array.isArray(arr)
将 return true
。
Array.isArray(obj)
将 return false
.