美文网首页
常用的小方法(持续添加中...)

常用的小方法(持续添加中...)

作者: 陈桑 | 来源:发表于2022-05-29 19:10 被阅读0次

    1、数组套对象排序(根据对象内的某个键进行排序)

    this.setArray(res.data.rows);//调用
    
    // 排序
    setArray(arr) {
      arr.sort(compare('orderIndex'));//orderIndex 用作排序的键
        function compare(keys) {
          return function (a, b) {
          return a[keys] - b[keys]
        }
      }
      return arr
    },
    

    2、数组套对象去重(根据某个键值进行去重)

    var arr = [{from: '张三',to: '河南'},{from: '李四',to: '河北'},{from: '李四',to: '北京'},{from: '李四',to: '南京'},]
    function setArr(arr) {
      const res = new Map();
      // form:需要根据哪个字段进行去重
      return arr.filter((a) => !res.has(a.from) && res.set(a.from, 1))
     }
    console.log(setArr(arr))
    

    3、获取当前之前某天或者当前之后某天(例:当天是2022-08-04)

    function getDay(day) {
        var today = new Date();
        var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
        today.setTime(targetday_milliseconds); //注意,这行是关键代码
        var tYear = today.getFullYear();
        var tMonth = today.getMonth();
        var tDate = today.getDate();
        tMonth = doHandleTime(tMonth + 1);
        tDate = doHandleTime(tDate);
        return tYear + "-" + tMonth + "-" + tDate;
    }
    function doHandleTime(t) {
        return t=t<10?'0'+t:t
    }
    console.log(getDay(-1))//2022-08-03
    console.log(getDay(0))//2022-08-04
    console.log(getDay(1))//2022-08-05
    

    4、判断当前设备是移动端还是PC端

    // 是app?
    this.isApp = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
    console.log(this.isApp ? '移动端' : 'PC端');
    

    5、省市区街道的JSON数据

    点击直接进入原网址

    6、解决H5上覆盖一层遮罩层,但是背景滚动的问题

    vue中:标签上添加 @touchmove.prevent

    <div class="phoneDialog" @touchmove.prevent></div>
    

    7、验证手机号和身份证号的格式

    // 手机号校验
    function phoneFun(params){
        const phoneRule = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/;
        const regex = new RegExp(phoneRule);
        return regex.test(params)
    }
    
    // 身份证校验
    function cardIdFun(params){
        const cardIdRule = /(^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$)|(^\d{6}(18|19|20)\d{2}(0[1-9]|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/;
        const regex = new RegExp(cardIdRule);
        return regex.test(params)
    }
    //使用
    let mobile = 155632569856
    console.log(phoneFun(mobile))
    

    8、验证当前是否是全屏状态

    function checkFull() {
      //判断浏览器是否处于全屏状态 (需要考虑兼容问题)
      let isFull =
        document.mozFullScreen ||
        document.fullScreen ||
        //谷歌浏览器及Webkit内核浏览器
        document.webkitIsFullScreen ||
        document.webkitRequestFullScreen ||
        document.mozRequestFullScreen ||
        document.msFullscreenEnabled;
      if (isFull === undefined) {
        isFull = false;
      }
      return isFull;
    }
    

    9、从一个对象数组中取出想要的某一个键

    来源,点击进入原地址

    let arr = [{a: 1,}, {b: 2,}, {c: 3}]
    let keys = arr.reduce((sum, item) => ([...new Set([...sum, ...Object.keys(item)])]), [])//['a','b','c']
    let res = arr[keys.indexOf('a')]
    console.log(res)//{a:1}
    
    //二:
    var all = [
        { f1: "v1", f2: "v2" },
        { f1: "v1", f2: "v2", f3: "v3" },
        { f1: "v1", f2: "v2", f3: "v3", f4: "v4" },
        { f1: "v1", f2: "v2", f3: "v3", f4: "v4", fn: "vn" }
    ];
    
    const output = Object.keys(Object.assign({}, ...all));
    console.log(output);//['f1', 'f2', 'f3', 'f4', 'fn']
    

    相关文章

      网友评论

          本文标题:常用的小方法(持续添加中...)

          本文链接:https://www.haomeiwen.com/subject/yvzoprtx.html