1024programmer Java JavaScript method to determine whether a variable is an object or an array_javascript skills

JavaScript method to determine whether a variable is an object or an array_javascript skills

typeof returns object

All data types in Javascript are objects in the strict sense, but in actual use we still have types. If you want to determine whether a variable is an array or an object, you can’t use typeof because it all returns object

The code is as follows:

var o = { ‘name’:’lee’ };
var a = [‘reg’,’blue’];

document.write( ‘ o typeof is ‘ + typeof o);
document.write( ‘
‘);
document.write( ‘ a typeof is ‘ + typeof a);

Execution:

The code is as follows:

o typeof is object
a typeof is object

Therefore, we can only give up this method. There are two ways to determine whether it is an array or an object

First, use typeof plus length attribute

Array has length attribute, object does not, and typeof array and object both return object, so we can judge this way

The code is as follows:

var o = { ‘name’:’lee’ };
var a = [‘reg’,’blue’];

var getDataType = function(o){
if(typeof o == ‘object’){
if( typeof o.length == ‘number’ ){
return ‘Array’;
}else{
return ‘Object’;
}
}else{
return ‘param is no object type’;
}
};

alert( getDataType(o) ); // Object
alert( getDataType(a) ); // Array
alert( getDataType(1) ); // param is no object type
alert( getDataType(true) ); // param is no object type
alert( getDataType(‘a’) ); // param is no object type

Second, use instanceof

Use instanceof to determine whether a variable is an array, such as:

The code is as follows:

var o = { ‘name’:’lee’ };
var a = [‘reg’,’blue’];

alert( a instanceof Array ); // true
alert( o instanceof Array ); // false

You can also determine whether it belongs to object

The code is as follows:

var o = { ‘name’:’lee’ };
var a = [‘reg’,’blue’];

alert( a instanceof Object ); // true
alert( o instanceof Object ); // true

But arrays also belong to objects, so both of the above are true. Therefore, when we use instanceof to determine whether the data type is an object or an array, we should first determine the array, and finally determine the object

The code is as follows:

var o = { ‘name’:’lee’ };
var a = [‘reg’,’blue’];

var getDataType = function(o){
if(o instanceof Array){
return ‘Array’
}else if( o instanceof Object ){
return ‘Object’;
}else{
return ‘param is no object type’;
}
};

alert( getDataType(o) ); // Object
alert( getDataType(a) ); // Array
alert( getDataType(1) ); // param is no object type
alert( getDataType(true) ); // param is no object type
alert( getDataType(‘a’) ); // param is no object type

If you do not prioritize Array, for example:

The code is as follows:

var o = { ‘name’:’lee’ };
var a = [‘reg’,’blue’];

var getDataType = function(o){
if(o instanceof Object){
return ‘Object’
}else if( o instanceof Array ){
return ‘Array’;
}else{
return ‘param is no object type’;
}
};

alert( getDataType(o) ); // Object
alert( getDataType(a) ); // Object
alert( getDataType(1) ); // param is no object type
alert( getDataType(true) ); // param is no object type
alert( getDataType(‘a’) ); // param is no object type

Then the array will also be judged as object.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/629633

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索