判断数组或对象

三组数据对比:

  • let arr1 = [12,34,1,1,55,66,1]
  • let newArr = new Set(arr1)
  • let newObj = {name: 'chris',age: '17',value: '99',grade: '大学一年级'}
   typeof * *.length  isNaN(*.length) Object.prototype.toString.call(*)   Array.isArray(*)  *.constructor  * instanceof Array
 arr1  object 7  false  [object Array]  true  ƒ Array() { [native code] }  true
 newArr  object undefined  true  [object Set]  false  ƒ Set() { [native code] }  false
 newObj  object undefined  true  [object Object]  false  ƒ Object() { [native code] }  false
  •  由表格可见,假定已有数据data,可以跟据以下几种方法进行判断data数据类型是否为数组
    • typeof data == Object && data.length
    • Object.prototype.toString.call(data) 
    • Array.isArray(data)
    • data.constructor
    • data instanceof Array
原文地址:https://www.cnblogs.com/simpleyou/p/14623098.html