• js常用方法集utils.js【持续更新】


      1 // 前端js公共方法集
      2 
      3 export default {
      4     // 配合element-ui更佳
      5 
      6     /*
      7      element时间修正
      8        参数:要修正的时间 
      9     */
     10     fixDateTime(val) {
     11         return new Date(Date.parse(new Date(val)) + 8 * 3600 * 1000)
     12     },
     13 
     14     /*
     15      附件格式过滤
     16        参数:1.el-upload的file,2.接受的文件类型后缀数组,如['.zip', 'png'],区分大小写
     17     */
     18     filterFile(file, arr) {
     19         let fileSize = file.size / 1000 / 1000
     20         if (fileSize > 25) {
     21             console.log('文件大小不能超过25M!')
     22             return false;
     23         }
     24         let fileName = file.name.lastIndexOf(".");//取到文件名开始到最后一个点的长度
     25         let fileNameLength = file.name.length;//取到文件名长度
     26         let fileFormat = '.' + file.name.substring(fileName + 1, fileNameLength);
     27         if (arr.indexOf(fileFormat) > -1) {
     28             return true
     29         } else {
     30             console.log('文件格式不对!')
     31             return false
     32         }
     33     },
     34 
     35     /*
     36      数组去重
     37        参数:1.待去重数组,2.过滤依据唯一key|对象数组时必须,String类型
     38     */
     39     filterArr(arr, key) {
     40         let newArr = []
     41         let tempArr = []
     42         arr.forEach(item => {
     43             let value = key ? item[key] : item
     44             if (tempArr.indexOf(value) < 0) {
     45                 newArr.push(item)
     46                 tempArr.push(value)
     47             }
     48         });
     49         return newArr
     50     },
     51 
     52     /*
     53      数组排序
     54        参数:1.待排序数组,2.是否升序(默认是),3.排序规则|0为数字(默认),1为字符串|非必须,4.排序依据唯一key|对象数组时必须,String类型
     55     */
     56     sortArr(arr, bool = true, rule = 0, key) {
     57         if (bool) {
     58             if (key) {
     59                 if (rule == 0) {
     60                     return arr.sort((a, b) => a[key] - b[key])
     61                 } else {
     62                     return arr.sort((a, b) => {
     63                         return a[key].localeCompare(b[key])
     64                     })
     65                 }
     66             } else {
     67                 if (rule == 0) {
     68                     return arr.sort((a, b) => a - b)
     69                 } else {
     70                     return arr.sort((a, b) => {
     71                         return a.localeCompare(b)
     72                     })
     73                 }
     74             }
     75         } else {
     76             if (key) {
     77                 if (rule == 0) {
     78                     return arr.sort((b, a) => a[key] - b[key])
     79                 } else {
     80                     return arr.sort((b, a) => {
     81                         return a[key].localeCompare(b[key])
     82                     })
     83                 }
     84             } else {
     85                 if (rule == 0) {
     86                     return arr.sort((b, a) => a - b)
     87                 } else {
     88                     return arr.sort((b, a) => {
     89                         return a.localeCompare(b)
     90                     })
     91                 }
     92             }
     93         }
     94     },
     95 
     96     /*
     97      对象数组添加字段
     98        参数:1.待添加数组,2.待添加字段key,3.待添加value|非必须,默认为null
     99     */
    100     addFieldArr(arr, key, value = null) {
    101         return arr.map(item => {
    102             let obj = {
    103                 ...item
    104             }
    105             obj[key] = value
    106             return obj
    107         })
    108     },
    109 
    110     /*
    111      对象数组合并相同key的data
    112        参数:1.待操作数组,2.参考key,3.待合并key
    113     */
    114     mergeKeyArr(arr, key1, key2) {
    115         arr = this.sortArr(arr, true, 1, key1)
    116         let newArr = []
    117         let tempValue = 0 // 临时存储数据
    118         let curName = arr[0][key1]
    119         // 合并相同curName的数据
    120         arr.forEach((item, index) => {
    121             if (item[key1] === curName) {
    122                 tempValue += item[key2]
    123             } else {
    124                 let obj = {
    125                     ...arr[index - 1]
    126                 }
    127                 obj[key2] = tempValue
    128                 newArr.push(obj)
    129                 curName = item[key1]
    130                 tempValue = item[key2]
    131             }
    132         })
    133         let obj = {
    134             ...arr[arr.length - 1]
    135         }
    136         obj[key2] = tempValue
    137         newArr.push(obj)
    138         return newArr
    139     },
    140 
    141     /*
    142      生成a-b随机数(默认0-255)
    143        参数:1.最小值|非必须,2.最大值|非必须
    144     */
    145     randomNumber(a = 0, b = 255) {
    146         return Math.round(a + Math.random() * (b - a))
    147     },
    148 
    149     /*
    150      生成随机rgb三颜色
    151        参数:1.是否可透明|非必须
    152     */
    153     randomColor(bool) {
    154         if (bool) {
    155             return `rgba(${this.randomNumber()}, ${this.randomNumber()}, ${this.randomNumber()}, .${this.randomNumber(0, 99)})`
    156         } else {
    157             return `rgb(${this.randomNumber()}, ${this.randomNumber()}, ${this.randomNumber()})`
    158         }
    159     },
    160 }

    更新日期:2020/12/25,如有错误,欢迎指出!

  • 相关阅读:
    ELK相关操作记录-运维笔记
    php执行普通shell命令
    rsync 限速同步文件
    mysql 压测工具启动报 error while loading shared libraries: libmysqlclient.so.20解决办法
    php连接SQL server 数据库测试php脚本
    mysql 5.6.24 主从配置(增加从库)
    Mysqldump参数大全(参数来源于mysql5.5.19源码)
    Linux下mysql-5.6重置root密码
    WSDL文档深入分析
    随笔
  • 原文地址:https://www.cnblogs.com/alt-fsh/p/14189934.html
Copyright © 2020-2023  润新知