js常用判断/数据处理,例如:判断数组中是否存在某个值,判断数据类型

1.判断数组中是否存在某个值

if (arr.indexOf(想查的数据) > -1 ) 有

2.判断数据类型

  1. typeof
    返回数据类型的字符串表达
    可以判断:undefined/数值/字符串/boolean/函数
    不能判断:null/object object/array
  2. instanceof
    用于判断对象的类型
    返回结果:true/false
    万物皆对象
  3. ===
    可以判断undefined,null
    综合万能判断:
/**
 * 判断数据类型
 * @param {any} val - 基本类型数据或者引用类型数据
 * @return {string} - 可能返回的结果有,均为小写字符串
 * number、boolean、string、null、undefined、array、object、function等
 */
export function getType(val){
    
    
  //判断数据是 null 和 undefined 的情况
  if (val == null) {
    
    
    return val + "";
  }
  return typeof(val) === "object" ?
      Object.prototype.toString.call(val).slice(8, -1).toLowerCase() :
      typeof(val);
}

3.去掉城市结尾的‘省市’

city.replace(/.+?(省|市|自治区|自治州|县|区)/g,''

4.自动补全,例如1补全成01:00

es6的padStart()       padEnd()
 '1'.padStart(2, '0') +':00'

5.判断文件类型

var filePath = "file:///storage/emulated/0/opmark/User/Pic/hangge.png";
var index= filePath.lastIndexOf(".");
var ext = filePath.substr(index+1);

Guess you like

Origin blog.csdn.net/qq_38974163/article/details/119533860