日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
今天,學會這10個JS代碼段就夠了!

 [[408192]]

用 apply 將數(shù)組各項添加到另一個數(shù)組

 
 
 
 
  1. const array = ['a', 'b']; 
  2. const elements = [0, 1, 2]; 
  3. array.push.apply(array, elements); 
  4. console.info(array); // ["a", "b", 0, 1, 2] 

函數(shù)只執(zhí)行一次

 
 
 
 
  1. function once (fn){ 
  2.   let called = false 
  3.   return function () { 
  4.     if (!called) { 
  5.       called = true 
  6.       fn.apply(this, arguments) 
  7.     } 
  8.   } 
  9.  
  10. function func (){ 
  11.     console.log(1); 
  12.  
  13. //調(diào)用 
  14. let onlyOnce = once(func); 
  15.  
  16. // 測試 
  17. onlyOnce(); // 1 
  18. onlyOnce(); // 不執(zhí)行 

防抖

 
 
 
 
  1. /** 
  2.  * 防抖 
  3.  * @param {Function} func 要執(zhí)行的回調(diào)函數(shù) 
  4.  * @param {Number} wait 延時的時間 
  5.  * @param {Boolean} immediate 是否立即執(zhí)行 
  6.  * @return null 
  7.  */ 
  8. let timeout; 
  9. function Debounce(func, wait = 3000, immediate = true) { 
  10.   // 清除定時器 
  11.   if (timeout !== null) clearTimeout(timeout); 
  12.   // 立即執(zhí)行,此類情況一般用不到 
  13.   if (immediate) { 
  14.     var callNow = !timeout; 
  15.     timeout = setTimeout(function() { 
  16.       timeout = null; 
  17.     }, wait); 
  18.     if (callNow) typeof func === 'function' && func(); 
  19.   } else { 
  20.     // 設置定時器,當最后一次操作后,timeout不會再被清除,所以在延時wait毫秒后執(zhí)行func回調(diào)方法 
  21.     timeout = setTimeout(function() { 
  22.       typeof func === 'function' && func(); 
  23.     }, wait); 
  24.   } 
  25.  
  26. Debounce(()=>console.log(1)); 

遞歸數(shù)組降為一維

 
 
 
 
  1. let children = [1, [2, 3], [4, [5, 6, [7, 8]]], [9, 10]]; 
  2. function simpleNormalizeChildren(children) { 
  3.             for (let i = 0; i < children.length; i++) { 
  4.                 if (Array.isArray(children[i])) { 
  5.                     children = Array.prototype.concat.apply([], children); 
  6.                     simpleNormalizeChildren(children) 
  7.                 } 
  8.             } 
  9.             return children; 
  10.         } 
  11.  
  12. console.log(simpleNormalizeChildren(children)); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

數(shù)組降維(二維降一維)

 
 
 
 
  1. function simpleNormalizeChildren(children) { 
  2.             for (let i = 0; i < children.length; i++) { 
  3.                 if (Array.isArray(children[i])) { 
  4.                     return Array.prototype.concat.apply([], children) 
  5.                 } 
  6.             } 
  7.             return children 
  8. let arrs = [['1'],['3']]; 
  9. const arr = simpleNormalizeChildren(arrs); 
  10. console.log(arr); // ['1','3'] 

使用可選鏈進行函數(shù)調(diào)用

 
 
 
 
  1. function doSomething(onContent, onError) { 
  2.   try { 
  3.    // ... do something with the data 
  4.   } 
  5.   catch (err) { 
  6.     onError?.(err.message); // 如果onError是undefined也不會有異常 
  7.   } 

檢測數(shù)組對象中是否有空值

 
 
 
 
  1. const data = [ 
  2.   { 
  3.    name:"maomin" 
  4.   }, 
  5.   { 
  6.     name:"" 
  7.   } 
  8. const arr = data.filter(item => 
  9.     Object.values(item).includes('') 
  10. ); 
  11.  
  12. console.log(arr.length>0?"有空值":"無空值"); // 有空值 

計算數(shù)組中每個元素出現(xiàn)的次數(shù)

 
 
 
 
  1. let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice']; 
  2.  
  3. let countedNames = names.reduce(function (allNames, name) { 
  4.   if (name in allNames) { 
  5.     allNames[name]++; 
  6.   } 
  7.   else { 
  8.     allNames[name] = 1; 
  9.   } 
  10.   return allNames; 
  11. }, {}); 
  12.  
  13. console.log(countedNames); //  { Alice: 2, Bob: 1, Tiff: 1, Bruce: 1 } 

按屬性對object分類

 
 
 
 
  1. let people = [ 
  2.   { name: 'Alice', age: 21 }, 
  3.   { name: 'Max', age: 20 }, 
  4.   { name: 'Jane', age: 20 } 
  5. ]; 
  6.  
  7. function groupBy(objectArray, property) { 
  8.   return objectArray.reduce(function (acc, obj) { 
  9.     let key = obj[property]; 
  10.     if (!acc[key]) { 
  11.       acc[key] = []; 
  12.     } 
  13.     acc[key].push(obj); 
  14.     return acc; 
  15.   }, {}); 
  16.  
  17. const groupedPeople = groupBy(people, 'age'); 
  18. console.log(groupedPeople); 
  19. // { 
  20. //   20: [ 
  21. //     { name: 'Max', age: 20 }, 
  22. //     { name: 'Jane', age: 20 } 
  23. //   ], 
  24. //   21: [{ name: 'Alice', age: 21 }] 
  25. // } 

將帶有分割符的字符串轉(zhuǎn)化成一個n維數(shù)組

 
 
 
 
  1.  const str = "A-2-12"; 
  2.  const str1 = str.split('-'); 
  3.  console.log(str1); 
  4.  const arr = str1.reverse().reduce((pre,cur,i) => { 
  5.  if(i==0) 
  6.   { pre.push(cur) 
  7.    return pre 
  8.  } 
  9.   return [cur,pre] 
  10. },[]) 
  11.   
  12. console.log(arr) // ["A"["B",["C"]]] 

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


網(wǎng)站欄目:今天,學會這10個JS代碼段就夠了!
標題來源:http://www.dlmjj.cn/article/dpgodos.html