成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

你可能會用到的JS工具函數

開發 開發工具
我們今天聊聊你可能會用到的JS工具函數,你能用到幾個?

[[403909]]

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.  
  9.  
  10.     window.onload = function () { 
  11.       const { createApp,ref } = Vue; 
  12.       const App = { 
  13.         template: ` 
  14.             <div>{{msg}}</div> 
  15.             <p>{{count}}</p> 
  16.             `, 
  17.         data(){ 
  18.               return { 
  19.                 msg:'maomin' 
  20.               } 
  21.         }, 
  22.         setup(){ 
  23.           let count = ref(0); 
  24.  
  25.           return { 
  26.             count 
  27.           } 
  28.         } 
  29.     } 
  30.      createApp(App).mount('#app'); 
  31.     } 

遞歸尋找操作(已刪除指定項為例)

  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.  
  21.       return result; 
  22.     }, 

遞歸數組,將數組為空設為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.  
  11.   // if obj is hit, it is in circular structure 
  12.   const hit = find(cache, c => c.original === obj) 
  13.   if (hit) { 
  14.     return hit.copy 
  15.   } 
  16.  
  17.   const copy = Array.isArray(obj) ? [] : {} 
  18.   // put the copy into cache at first 
  19.   // because we want to refer it in recursive deepCopy 
  20.   cache.push({ 
  21.     original: obj, 
  22.     copy 
  23.   }) 
  24.  
  25.   Object.keys(obj).forEach(key => { 
  26.     copy[key] = deepCopy(obj[key], cache) 
  27.   }) 
  28.  
  29.   return copy 
  30.  
  31. const objs = { 
  32.     name:'maomin'
  33.     age:'17' 
  34.  
  35. 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.  
  9.    channelInputLimit(e) { 
  10.        let key = e.key
  11.        // 不允許輸入‘e‘和‘.‘ 
  12.        if (key === 'e' || key === '.') { 
  13.            e.returnValue = false
  14.            return false
  15.        } 
  16.        return true
  17.    }, 

排序(交換位置)

  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}-owocgsc {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.  
  27.         if (typeof time === 'number' && time.toString().length === 10) { 
  28.             time = time * 1000; 
  29.         } 
  30.         date = new Date(time); 
  31.     } 
  32.     const formatObj = { 
  33.         y: date.getFullYear(), 
  34.         m: date.getMonth() + 1, 
  35.         d: date.getDate(), 
  36.         h: date.getHours(), 
  37.         i: date.getMinutes(), 
  38.         s: date.getSeconds(), 
  39.         a: date.getDay() 
  40.     }; 
  41.     const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => { 
  42.         const value = formatObj[key]; 
  43.         // Note: getDay() returns 0 on Sunday 
  44.         if (key === 'a') { 
  45.             return ['日''一''二''三''四''五''六'][value]; 
  46.         } 
  47.         return value.toString().padStart(2, '0'); 
  48.     }); 
  49.     return time_str; 

本文轉載自微信公眾號「前端歷劫之路」,可以通過以下二維碼關注。轉載本文請聯系前端歷劫之路公眾號。

 

責任編輯:武曉燕 來源: 前端歷劫之路
相關推薦

2011-08-31 16:01:33

2021-08-06 07:55:55

黑客工具網站臨時手機號

2015-04-08 09:54:41

OpenStack資源私有云部署

2020-07-06 07:48:16

MySQL細節SQL

2020-12-08 05:56:51

JS工具函數

2018-07-10 11:05:18

開發者技能命令

2018-07-10 10:45:00

規范Commit項目

2016-03-16 11:20:47

2020-10-08 18:14:15

碼農Git命令

2020-03-09 10:10:02

AI 數據人工智能

2025-06-20 08:14:55

2022-04-24 10:51:57

Python漏洞

2010-06-21 10:34:03

職場信號被炒

2017-03-23 16:03:01

2014-02-18 10:59:52

nftablesLinux 3.13

2017-11-21 10:15:00

2017-11-23 11:56:00

2020-10-13 08:40:01

CSS多行多列布局

2020-09-01 14:17:03

WindowsDefender微軟

2019-03-22 08:00:01

Git命令GitHub
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 91免费在线视频 | 欧美日韩国产在线观看 | 欧美日产国产成人免费图片 | 国产精品日韩欧美一区二区三区 | 国产视频亚洲视频 | 亚洲精美视频 | av在线视| 国产三级在线观看播放 | 我要看黄色录像一级片 | 国产色99 | 国产成人亚洲精品自产在线 | 亚洲男人网 | 国产精品美女 | 国产成人免费视频网站视频社区 | 天天干天天想 | 欧美三区在线观看 | 国产区在线观看 | 人人擦人人 | 国产成人高清视频 | 日韩和的一区二在线 | www.天天干.com| 精久久久 | 一本色道精品久久一区二区三区 | 国产精品18hdxxxⅹ在线 | 亚洲免费影院 | 黄色网址大全在线观看 | 欧美精品成人影院 | 一区二区三区四区在线播放 | 日韩a| 天天干天天谢 | 精品一区二区三区在线观看国产 | 欧洲一级毛片 | 亚洲成人免费视频在线 | 精品国产99 | 99九九视频| 久热爱| 美女天天干天天操 | 国产一区二区在线91 | 日韩一区二区av | 国产96在线| 看毛片网站 |