首页 > 编程语言 >判断JavaScript数据是否为空

判断JavaScript数据是否为空

时间:2023-02-20 17:14:30浏览次数:42  
标签:case 判断 obj true JavaScript break flag 为空 type

getObjType = (obj) => {
    const type = Object.prototype.toString.call(obj)
    const array = type.split(/\s/g)
    return array[1].replace(']', '')
  }
 

isEmpty = (obj) => {
    const type = getObjType(obj)
    console.log('type', type)
    let flag = false
    switch (type) {
      case 'Null':
        flag = true
        break
      case 'Undefined':
        flag = true
        break
      case 'String':
        if (obj.trim() === '') {
          flag = true
        }
        break
      case 'Array':
        if (obj.length === 0) {
          flag = true
        }
        break
      default:
        flag = false
    }
    return flag
  }

标签:case,判断,obj,true,JavaScript,break,flag,为空,type
From: https://www.cnblogs.com/luckyitape/p/17138122.html

相关文章