常用的utils

常用的utils

//中止请求后端接口,axios取消请求
export function stopFun(){
    //遍历数组中的接口,删除请求
    try{
        window._axiosPromiseArr.forEach((ele, index) => {
            ele.cancel();
            delete window._axiosPromiseArr[index];
        });
    }catch(e){
        console.log(e)
    }
}


   //倒计时时间格式化    示例 08:23:21
    getFormData: {
      var t = this.lastTime  //7569
      var result = []
      if (t > 0) {
        //天
        result.push(Math.floor(t / (3600 * 24)))
        //时
        result.push(Math.floor((t % (3600 * 24)) / 3600))
        //分
        result.push(Math.floor((t % 3600) / 60))
        //秒
        result.push(Math.floor(t % 60))
      } else {
        result[1] = 0
        result[2] = 0
        result[3] = 0
      }
      //补0
      result.forEach(function (item, index) {
        if (item < 10 && index > 0) {
          result[index] = '0' + result[index]
        }
      })
      return result
    }/**
    * 获取地址栏参数
   */
    getUrlParam: function (paramName) {
      var reg = new RegExp("(^|&)" + paramName + "=([^&]*)(&|$)", "i"),
        arr = window.location.search.substr(1).match(reg);
      if (arr) {
        return arr[2];
      }
      return null;
    },

 //对象数组排序方法
      enrArray.sort(this.conpare('orderId'))
  
   
    compare(prop){
      return function(a,b){
        var val1 = a[prop]
        var val2 = b[prop]
        return val1 - val2
      }
    }



// var points = [40, 100, 1, 5, 25, 10];
// points.sort(function(a, b){return a-b});


  //格式化状态对应中文
    statusFilter(val){
      let obj ={
        1 : "直播前",
        2 : "直播中",
        3 : "直播结束"
      }
      return obj[val]
    }

//高亮变色
setHeighlight() {
    if (this.search && this.search.length > 0) {
      let that = this
      this.searchList.forEach(item => {
        //匹配关键字正则
        let replaceReg = new RegExp(that.search, 'ig')
        //高亮替换v-html
        let replaceStr = '<span class="search_text">' + that.search + '</span>'
        //替换
        item.subject = item.subject.replace(replaceReg, replaceStr)
        return item
      })
    }
  }

//手写深拷贝
let obj = {
   a: undefined,
   b: function () {},
   c: Symbol("foo"),
   d: null,
   e: "hello",
   arr: [{
           uname: "lili",
           arr2: [1, 2, 3]
       },
       [1, 2, 3]
   ]
}
function deepCopy(obj, type = {}) {//type设置一个默认的空对象
   for (const key in obj) {//遍历obj中的属性
   // obj[key]表示对应属性名的属性,判断一下这个属性是基本数据还是引用数据
       if (typeof obj[key] === "object") {
       //type[key] 判断一下这个属性是数组还是对象,对应的创建一个空数组或者对象
           type[key] = Array.isArray(obj[key]) ? [] : {};
           //如果是hi引用数据类型就继续调用本身查找里面的属性
           deepCopy(obj[key], type[key])
       } else {
       //如果是基本数据就直接拷贝出属性对应的值放在type
           type[key] = obj[key];
       }
   }
   return type;//最后返回这个type对象
}

let x = new deepCopy(obj);//传递obj对象
x.arr[1][0] = "hi";//改变深层中的内容会发现对另外一个没有影响
console.log(x,obj);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值