typescript剔除表单中的空值(包含判断字符串为空或undefined)

 public static exceptNull = (params: any): any => {
    let data: any = {};
    for (const item in params) {
      if (params.hasOwnProperty(item)) {
        if (StringUtils.isNotEmpty(params[item])) {
          if (typeof params[item] === 'object') {
            let a = params[item];
            data[item] = JSON.stringify(a);
          } else {
            data[item] = params[item];
          }
        }
      }
    }
    return data;
  };
  
 
  

判断字符串是否为空或undefined

	/**
	 * 判断字符串是否为空或undefined,不判断为0,不判断为false
	 * @param str
	 * @returns {boolean}
	 */
	public static isEmpty = (str: any): boolean => {
	  if (
	    str === null ||
			str === '' ||
			str === undefined ||
			str.length === 0
	  ) {
	    return true
	  } else {
	    return false
	  }
	};
	public static isNotEmpty = (str: any): boolean => {
	  if (
	    str === null ||
			str === '' ||
			str === undefined ||
			str.length === 0
	  ) {
	    return false
	  } else {
	    return true
	  }
	};
}

export default StringUtils

你可能感兴趣的:(typescript剔除表单中的空值(包含判断字符串为空或undefined))