我正在尝试编写一个函数,它要么接受字符串列表,要么接受单个字符串。如果它是一个字符串,那么我想把它转换成一个只有一个项的数组,这样我就可以在上面循环,而不用担心出错。
那么如何检查变量是否为数组?
我正在尝试编写一个函数,它要么接受字符串列表,要么接受单个字符串。如果它是一个字符串,那么我想把它转换成一个只有一个项的数组,这样我就可以在上面循环,而不用担心出错。
那么如何检查变量是否为数组?
当前回答
您可以使用此函数获取数据类型。
var myAr = [1,2];
checkType(myAr);
function checkType(data) {
if(typeof data ==='object') {
if(Object.prototype.toString.call(data).indexOf('Array') !== (-1)) {
return 'array';
} else {
return 'object';
}
} else {
return typeof data;
}
}
if(checkType(myAr) === 'array') {
console.log('yes, it is an array')
};
其他回答
此函数将几乎所有内容转换为数组:
function arr(x) {
if(x === null || x === undefined) {
return [];
}
if(Array.isArray(x)) {
return x;
}
if(isString(x) || isNumber(x)) {
return [x];
}
if(x[Symbol.iterator] !== undefined || x.length !== undefined) {
return Array.from(x);
}
return [x];
}
function isString(x) {
return Object.prototype.toString.call(x) === "[object String]"
}
function isNumber(x) {
return Object.prototype.toString.call(x) === "[object Number]"
}
它使用了一些较新的浏览器功能,因此您可能需要对其进行多填充以获得最大支持。
示例:
> arr(null);
[]
> arr(undefined)
[]
> arr(3.14)
[ 3.14 ]
> arr(1/0)
[ Infinity ]
> gen = function*() { yield 1; yield 2; yield 3; }
[Function: gen]
> arr(gen())
[ 1, 2, 3 ]
> arr([4,5,6])
[ 4, 5, 6 ]
> arr("foo")
[ 'foo' ]
N.B.字符串将被转换为具有单个元素的数组,而不是字符数组。如果您希望使用其他方式,请删除isString复选框。
我在这里使用了Array.isArray,因为它是最健壮的,也是最简单的。
我首先检查您的实现是否支持isArray:
if (Array.isArray)
return Array.isArray(v);
也可以尝试使用instanceof运算符
v instanceof Array
这个问题只有一行解决方案
x instanceof Array
其中x是变量,如果x是数组,则返回true,如果不是,则返回false。
也存在其他方法来检查,但我更喜欢以下方法作为我的最佳检查方法(因为您可以轻松检查其他对象的类型)。
> a = [1, 2]
[ 1, 2 ]
>
> Object.prototype.toString.call(a).slice(8,).replace(/\]$/, '')
'Array'
>
> Object.prototype.toString.call([]).slice(8,-1) // best approach
'Array'
解释(节点REPL上的简单示例)»
> o = {'ok': 1}
{ ok: 1 }
> a = [1, 2]
[ 1, 2 ]
> typeof o
'object'
> typeof a
'object'
>
> Object.prototype.toString.call(o)
'[object Object]'
> Object.prototype.toString.call(a)
'[object Array]'
>
对象或阵列»
> Object.prototype.toString.call(o).slice(8,).replace(/\]$/, '')
'Object'
>
> Object.prototype.toString.call(a).slice(8,).replace(/\]$/, '')
'Array'
>
空或未定义»
> Object.prototype.toString.call(undefined).slice(8,).replace(/\]$/, '')
'Undefined'
> Object.prototype.toString.call(null).slice(8,).replace(/\]$/, '')
'Null'
>
字符串»
> Object.prototype.toString.call('ok').slice(8,).replace(/\]$/, '')
'String'
编号»
> Object.prototype.toString.call(19).slice(8,).replace(/\]$/, '')
'Number'
> Object.prototype.toString.call(19.0).slice(8,).replace(/\]$/, '')
'Number'
> Object.prototype.toString.call(19.7).slice(8,).replace(/\]$/, '')
'Number'
>
我很感谢@mpen建议使用-1代替正则表达式,如下所示。
> Object.prototype.toString.call(12).slice(8,-1)
'Number'
>
> Object.prototype.toString.call(12.0).slice(8,-1)
'Number'
>
> Object.prototype.toString.call([]).slice(8,-1)
'Array'
> Object.prototype.toString.call({}).slice(8,-1)
'Object'
>
> Object.prototype.toString.call('').slice(8,-1)
'String'
>
在现代浏览器中,您可以执行以下操作:
Array.isArray(obj)
(支持Chrome 5、Firefox 4.0、Internet Explorer 9、Opera 10.5和Safari 5)
为了实现向后兼容性,可以添加以下内容:
// Only implement if no native implementation is available
if (typeof Array.isArray === 'undefined') {
Array.isArray = function(obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
}
};
如果使用jQuery,可以使用jQuery.isArray(obj)或$.isArra(obj)。如果使用Undercore.js,可以使用_.isArray(obj。
如果不需要检测在不同帧中创建的阵列,也可以只使用instanceof:
obj instanceof Array