我正在尝试编写一个函数,它要么接受字符串列表,要么接受单个字符串。如果它是一个字符串,那么我想把它转换成一个只有一个项的数组,这样我就可以在上面循环,而不用担心出错。

那么如何检查变量是否为数组?


当前回答

也存在其他方法来检查,但我更喜欢以下方法作为我的最佳检查方法(因为您可以轻松检查其他对象的类型)。

> 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'
>

其他回答

jQuery还提供了一个$.isArray()方法:

var a=[“a”,“AA”,“AAA”];if($.isArray(a)){alert(“a是一个数组!”);}其他{alert(“a不是数组!”);}<script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js“></script>

假设您有以下阵列:

var arr = [1,2,3,4,5];

JavaScript(新浏览器和旧浏览器):

function isArray(arr) {
  return arr.constructor.toString().indexOf("Array") > -1;
}

or

function isArray(arr) {
  return arr instanceof Array;
}

or

function isArray(arr) {
  return Object.prototype.toString.call(arr) === '[object Array]';
}

然后这样称呼:

isArray(arr);

JavaScript(Internet Explorer 9+、Chrome 5+、Firefox 4+、Safari 5+和Opera 10.5+)

Array.isArray(arr);

jQuery:

$.isArray(arr);

角度:

angular.isArray(arr);

Undercore.js和Lodash:

_.isArray(arr);

您可以使用Array.isArray()。这里是一个polyfill:

if (Array.isArray == null) {
  Array.isArray = (arr) => Object.prototype.toString.call(arr) === "[object Array]"
}

您可以使用此函数获取数据类型。

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')
};

我会制作一个函数来测试您正在处理的对象的类型。。。

函数whatAmI(me){return Object.pr原型.toString.call(me).split(/\W/)[2];}//测试控制台日志(whatAmI([“瞄准”,“@”]),whatAmI({生活:4,呼吸:4}),whatAmI(函数(ing){return ing+“到全局窗口”}),我(“要和你做什么?”));//输出:数组对象函数字符串

然后你可以写一个简单的if语句。。。

if(whatAmI(myVar) === "Array"){
    // do array stuff
} else { // could also check `if(whatAmI(myVar) === "String")` here to be sure
    // do string stuff
}