Home > WeChat Applet > Mini Program Development > How to determine if a message is blank in the frontend of the mini program

How to determine if a message is blank in the frontend of the mini program

angryTom
Release: 2020-03-26 11:39:35
Original
2762 people have browsed it

How to determine if a message is blank in the frontend of the mini program

How to determine if the message is blank in the frontend of the mini program

Method: You can encapsulate a function that determines whether the object is empty, such as isBlank in the following code.

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
  }
}
Copy after login

Use isBlank to judge whether the message is empty:

if(isBlank(message)){
    console.log('消息为空')
}else{
    console.log('消息:' + message)
}
Copy after login

PHP Chinese website, a large number of free small program development tutorials, welcome to learn!

The above is the detailed content of How to determine if a message is blank in the frontend of the mini program. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template