判断变量是否是数组

两种方法

数组:var arr = [1,2,3];

1. ECMA5的新方法isArray(),对就浏览器不支持

  使用方法:Array.isArray(arr) ->true or false

2. Object.prototype.toString.call(value)

  使用方法:(Object.prototype.toString.call(arr) == "[object array]" ) == true

综合使用

  function isArray(value){ 
    if (typeof Array.isArray === "function") { 
      return Array.isArray(value); 
    }else{ 
      return Object.prototype.toString.call(value) === "[object Array]"; 
    } 
  } 

knockout中检测是否维数组的方法

if (typeof initialValues != 'object' || !('length' in initialValues)){
}

  

原文地址:https://www.cnblogs.com/huntaheart/p/4024622.html