本网站(662p.com)打包出售,且带程序代码数据,662p.com域名,程序内核采用TP框架开发,需要联系扣扣:2360248666 /wx:lianweikj
精品域名一口价出售:1y1m.com(350元) ,6b7b.com(400元) , 5k5j.com(380元) , yayj.com(1800元), jiongzhun.com(1000元) , niuzen.com(2800元) , zennei.com(5000元)
需要联系扣扣:2360248666 /wx:lianweikj
你可能会用到的JS工具函数
zhuxiaoqiang · 187浏览 · 发布于2021-06-07 +关注

Vue3在script标签中引入


  1. const oDiv = document.createElement('div'); 

  2.     const oScript = document.createElement('script'); 

  3.     oDiv.setAttribute('id', 'app'); 

  4.     oScript.type = 'text/javascript'; 

  5.     oScript.src = "https://unpkg.com/vue@next"; 

  6.     document.body.appendChild(oDiv); 

  7.     document.body.appendChild(oScript); 



  8.     window.onload = function () { 

  9.       const { createApp,ref } = Vue; 

  10.       const App = { 

  11.         template: ` 

  12.             <div>{{msg}}</div> 

  13.             <p>{{count}}</p> 

  14.             `, 

  15.         data(){ 

  16.               return { 

  17.                 msg:'maomin' 

  18.               } 

  19.         }, 

  20.         setup(){ 

  21.           let count = ref(0); 


  22.           return { 

  23.             count 

  24.           } 

  25.         } 

  26.     } 

  27.      createApp(App).mount('#app'); 

  28.     } 

递归寻找操作(已删除指定项为例)


  1. // 递归寻找 

  2.     recursion(data, id) { 

  3.       let result; 

  4.       if (!data) { 

  5.         return; 

  6.       } 

  7.       for (let i = 0; i < data.length; i++) { 

  8.         let item = data[i]; 

  9.         if (item.breakerId === id) { 

  10.           result = item; 

  11.           data.splice(i, 1); 

  12.           break; 

  13.         } else if (item.childrenBranch && item.childrenBranch.length > 0) { 

  14.           result = this.recursion(item.childrenBranch, id); 

  15.           if (result) { 

  16.             return result; 

  17.           } 

  18.         } 

  19.       } 


  20.       return result; 

  21.     }, 

递归数组,将数组为空设为undefined


  1. function useTree(data) { 

  2.        for (let index = 0; index < data.length; index++) { 

  3.          const element = data[index]; 

  4.          if (element.childrenBranch.length < 1) { 

  5.            element.childrenBranch = undefined; 

  6.          } else { 

  7.            useTree(element.childrenBranch); 

  8.          } 

  9.        } 

  10.        return data; 

  11.      } 

数组对象中相同属性值的个数


  1. group(arr) { 

  2.       var obj = {}; 

  3.       if (Array.isArray(arr)) { 

  4.         for (var i = 0; i < arr.length; ++i) { 

  5.           var isNew = arr[i].isNew; 

  6.           if (isNew in obj) obj[isNew].push(arr[i]); 

  7.           else obj[isNew] = [arr[i]]; 

  8.         } 

  9.       } 

  10.       return obj; 

  11.     }, 

  12.     max(obj) { 

  13.       var ret = 0; 

  14.       if (obj && typeof obj === "object") { 

  15.         for (var key in obj) { 

  16.           var length = obj[key].length; 

  17.           if (length > ret) ret = length; 

  18.         } 

  19.       } 

  20.       return ret; 

  21.     }, 

  22. var data = [ 

  23.     { 

  24.      addr: "1", 

  25.      isNew: false, 

  26.     }, 

  27.     { 

  28.      addr: "2", 

  29.      isNew: false, 

  30.     } 

  31. max(group(data) // 2 

检测版本是vue3


  1. import { h } from 'vue'; 

  2. const isVue3 = typeof h === 'function'; 

  3. console.log(isVue3) 

检测数据对象中是否有空对象


  1. let arr = [{},{name:'1'}] 

  2. const arr = this.bannerList.filter(item => 

  3.        item == null || item == '' || JSON.stringify(item) == '{}' 

  4. ); 

  5.  console.log(arr.length > 0 ? '不通过' : '通过') 

深拷贝


  1.  /* @param {*} obj 

  2.  * @param {Array<Object>} cache 

  3.  * @return {*} 

  4.  */ 

  5. function deepCopy (obj, cache = []) { 

  6.   // just return if obj is immutable value 

  7.   if (obj === null || typeof obj !== 'object') { 

  8.     return obj 

  9.   } 


  10.   // if obj is hit, it is in circular structure 

  11.   const hit = find(cache, c => c.original === obj) 

  12.   if (hit) { 

  13.     return hit.copy 

  14.   } 


  15.   const copy = Array.isArray(obj) ? [] : {} 

  16.   // put the copy into cache at first 

  17.   // because we want to refer it in recursive deepCopy 

  18.   cache.push({ 

  19.     original: obj, 

  20.     copy 

  21.   }) 


  22.   Object.keys(obj).forEach(key => { 

  23.     copy[key] = deepCopy(obj[key], cache) 

  24.   }) 


  25.   return copy 


  26. const objs = { 

  27.     name:'maomin', 

  28.     age:'17' 


  29. console.log(deepCopy(objs)); 

h5文字转语音


  1. speech(txt){ 

  2.     var synth = null; 

  3.     var msg = null; 

  4.     synth = window.speechSynthesis; 

  5.     msg = new SpeechSynthesisUtterance(); 

  6.     msg.text = txt; 

  7.     msg.lang = "zh-CN"; 

  8.     synth.speak(msg); 

  9.     if(window.speechSynthesis.speaking){ 

  10.       console.log("音效有效"); 

  11.      } else { 

  12.      console.log("音效失效"); 

  13.      } 

  14.  } 

模糊搜索


  1. recursion(data, name) { 

  2.             let result; 

  3.             if (!data) { 

  4.                 return; 

  5.             } 

  6.             for (var i = 0; i < data.length; i++) { 

  7.                 let item = data[i]; 

  8.                 if (item.name.toLowerCase().indexOf(name) > -1) { 

  9.                     result = item; 

  10.                     break; 

  11.                 } else if (item.children && item.children.length > 0) { 

  12.                     result = this.recursion(item.children, name); 

  13.                     if (result) { 

  14.                         return result; 

  15.                     } 

  16.                 } 

  17.             } 

  18.             return result; 

  19.         }, 

  20.         onSearch(v) { 

  21.             if (v) { 

  22.                 if (!this.recursion(this.subtable, v)) { 

  23.                     this.$message({ 

  24.                         type: 'error', 

  25.                         message: '搜索不到', 

  26.                     }); 

  27.                 } else { 

  28.                     this.tableData = [this.recursion(this.subtable, v)]; 

  29.                 } 

  30.             } 

  31.         }, 

input 数字类型


  1.   <el-input 

  2.                    v-model.number="form.redVal" 

  3.                    type="number" 

  4.                    @keydown.native="channelInputLimit" 

  5.                    placeholder="请输入阈值设定" 

  6.                    maxlength="8" 

  7. ></el-input> 


  8.    channelInputLimit(e) { 

  9.        let key = e.key; 

  10.        // 不允许输入‘e‘和‘.‘ 

  11.        if (key === 'e' || key === '.') { 

  12.            e.returnValue = false; 

  13.            return false; 

  14.        } 

  15.        return true; 

  16.    }, 

排序(交换位置)


  1. const list = [1,2,3,4,5,6]; 

  2.     function useChangeSort (arr,oldIndex,newIndex){ 

  3.         const targetRow = arr.splice(oldIndex, 1)[0]; 

  4.         const targetRow1 = arr.splice(newIndex, 1)[0]; 

  5.         arr.splice(newIndex, 0, targetRow); 

  6.         arr.splice(oldIndex, 0, targetRow1); 

  7.         return arr 

  8.     } 

  9.     console.log(useChangeSort(list,5,0)); // [6, 2, 3, 4, 5, 1] 

格式化时间


  1. /** 

  2.  * Parse the time to string 

  3.  * @param {(Object|string|number)} time 

  4.  * @param {string} cFormat 

  5.  * @returns {string | null} 

  6.  */ 

  7. export function parseTime(time, cFormat) { 

  8.     if (arguments.length === 0 || !time) { 

  9.         return null; 

  10.     } 

  11.     const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'; 

  12.     let date; 

  13.     if (typeof time === 'object') { 

  14.         date = time; 

  15.     } else { 

  16.         if (typeof time === 'string') { 

  17.             if (/^[0-9]+$/.test(time)) { 

  18.                 // support "1548221490638" 

  19.                 time = parseInt(time); 

  20.             } else { 

  21.                 // support safari 

  22.                 // https://stackoverflow.com/questions/4310953/invalid-date-in-safari 

  23.                 time = time.replace(new RegExp(/-/gm), '/'); 

  24.             } 

  25.         } 


  26.         if (typeof time === 'number' && time.toString().length === 10) { 

  27.             time = time * 1000; 

  28.         } 

  29.         date = new Date(time); 

  30.     } 

  31.     const formatObj = { 

  32.         y: date.getFullYear(), 

  33.         m: date.getMonth() + 1, 

  34.         d: date.getDate(), 

  35.         h: date.getHours(), 

  36.         i: date.getMinutes(), 

  37.         s: date.getSeconds(), 

  38.         a: date.getDay() 

  39.     }; 

  40.     const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => { 

  41.         const value = formatObj[key]; 

  42.         // Note: getDay() returns 0 on Sunday 

  43.         if (key === 'a') { 

  44.             return ['日', '一', '二', '三', '四', '五', '六'][value]; 

  45.         } 

  46.         return value.toString().padStart(2, '0'); 

  47.     }); 

  48.     return time_str; 


相关推荐

PHP实现部分字符隐藏

沙雕mars · 1324浏览 · 2019-04-28 09:47:56
Java中ArrayList和LinkedList区别

kenrry1992 · 907浏览 · 2019-05-08 21:14:54
Tomcat 下载及安装配置

manongba · 968浏览 · 2019-05-13 21:03:56
JAVA变量介绍

manongba · 961浏览 · 2019-05-13 21:05:52
什么是SpringBoot

iamitnan · 1086浏览 · 2019-05-14 22:20:36
加载中

0评论

评论
我爱编程,我爱工作,更爱生活
小鸟云服务器
扫码进入手机网页