您的位置:首页 > 移动开发 > Objective-C

小程序或js,判断变量(Undefined String Array Object)是否为空

2017-11-25 10:52 344 查看
关键代码

function isBlank(str){
if (Object.prototype.toString.call(str) ==='[object Undefined]'){//空
return true
} else if (
Object.prototype.toString.call(str) === '[object String]' ||
Object.prototype.toString.call(str) === '[object Array]') { //字条串或数组
return str.length==0?true:false
} else if (Object.prototype.toString.call(str) === '[object Object]') {
return JSON.stringify(str)=='{}'?true:false
}else{
return true
}

}


测试结果



小程序使用方法

utils.js文件


const formatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()

return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

/**
* 用于判断空,Undefined String Array Object
*/
function isBlank(str){ if (Object.prototype.toString.call(str) ==='[object Undefined]'){//空 return true } else if ( Object.prototype.toString.call(str) === '[object String]' || Object.prototype.toString.call(str) === '[object Array]') { //字条串或数组 return str.length==0?true:false } else if (Object.prototype.toString.call(str) === '[object Object]') { return JSON.stringify(str)=='{}'?true:false }else{ return true } }

/**
* 导出
*/
module.exports = {
formatTime: formatTime,
String:{
isBlank: isBlank
}
}


其它js文件使用

import { String } from '../../utils/util.js';
var xx = '';
if( String.isBlank(xx))
//xx is blank
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息