自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

解析與盤點數(shù)組array的5類22種方法

開發(fā) 前端
JS是唯一幾乎全面支持函數(shù)式編程的流行語言,而函數(shù)編程的起點是處理數(shù)組。因此,我們首先來盤點 array 數(shù)組的5類22種方法。

[[388247]]

 JS是唯一幾乎全面支持函數(shù)式編程的流行語言,而函數(shù)編程的起點是處理數(shù)組。因此,我們首先來盤點 array 數(shù)組的5類22種方法。

一、數(shù)組變形 Transform (函數(shù)范式的純函數(shù))

首先列出對數(shù)組變形操作的沒有side-effects的函數(shù)。

1) reduce 2) map 3) flat 4) flatMap 5) fill 6) forEach。其中 forEach 非 pure-function, 但屬于隱性迭代方法,故而分類在此。

  1. // 1.reduce 
  2. let total = [ 0, 1, 2, 3 ].reduce( ( acc, cur ) => acc + cur, 
  3.                                   0); 
  4. // 2.map  
  5. const maped = [4, 7, 9, 18].map(x => x * 2); 
  6.  
  7. // 3.flat 
  8. let arr2 = [5, 2, 3].flat(); 
  9.  
  10. // 4.flatMap 
  11. [5, 8, 9, 6].flatMap(x => [[x * 2]]); 
  12.   
  13. // 5.fill 非純函數(shù),但是因為常用于構(gòu)建 range 用于迭代,因此分類到這里 
  14. Array(5).fill(0) 
  15.  
  16. // 6.forEach 非純函數(shù),作為 map 處理有 side-effect 問題的替代方案。 
  17. ['x''y''z'].forEach(el => console.log(el)); 

 二、數(shù)組邏輯判斷 logic predicates(函數(shù)范式的純函數(shù))

函數(shù)范式的六個methods之后,我們繼續(xù)考察用于邏輯判斷的高階函數(shù):

1) filter 2) find 3) findIndex 4) includes 5) indexOf 6) some 7) every 以及我們可以自己構(gòu)建頗有幫助 range 與 not。

其中 include 是 find 應用于一個元素,而 indexOf 則是 findIndex 用于一個元素。

  1. // 1.filter 
  2. [23, 76, 98, 101].filter( v => v > 30 && v < 100); //[ 76, 98 ] 
  3.  
  4. // 2.find 只需要單個元素則用 find 
  5. [23, 76, 98, 101].find( v => v > 30 && v < 100); // 76  
  6.  
  7. // 3.findIndex 查找單個元素的index 
  8. [23, 76, 98, 101].findIndex( v => v > 30 && v < 100); // 1 
  9.  
  10. // 4.includes 是 find 查找特定元素 
  11. [23, 76, 98, 101].includes(77) // false 
  12.  
  13. // 5.indexOf 是 findIndex 查找某個特定元素的 index,返回值為 -1  
  14. [23, 76, 98, 101].indexOf(77) // -1 
  15.  
  16. // 6.some 
  17. [23, 76, 98, 101].some(v => v > 30 && v < 100) //true 
  18.  
  19. // 7.every 
  20. [23, 76, 98, 101].every(v => v > 30 && v < 100) //false 

 三、非函數(shù)式的數(shù)組變形(純函數(shù))

以上兩組12個函數(shù)均為函數(shù)范式編程的純函數(shù)。接下來考察,其他對數(shù)組變形的純函數(shù)。(純函數(shù)是指沒有side-effect副作用的函數(shù)):

1) concat 2) join 3) slice 4) splice (非純函數(shù),將會修改原數(shù)組,放在此處只與slice對比,作為提醒)

  1. // 1.concat  
  2. ['x''y''z'].concat([9, 8, 7]); 
  3.  
  4. // 2.join 
  5. ['x''y''z'].join(","); 
  6.  
  7. // 3.slice 
  8. ['x''y''z'].slice(1, 3); 
  9.  
  10. // 4.splice放到第四組中,此處只為提醒與slice相對比。 

 四、操作數(shù)據(jù)結(jié)構(gòu) (非純函數(shù))

Array可以作為兩種抽象結(jié)構(gòu)數(shù)據(jù)的載體:分別為 stack 和 queue。

1) push 2) pop 3) shift 4) unshift 5)splice(splice屬于特殊方法,因為更改了原數(shù)組,放在此處)

  1. let arr = [23, 76, 98, 101]; 
  2.  
  3. // 1. push 元素添加到尾部 
  4. > arr.push(120) 
  5. > console.log(arr) 
  6. [ 23, 76, 98, 101, 120 ] 
  7.  
  8. // 2.pop 以上 push 與 pop 組合構(gòu)建 stack 數(shù)據(jù)結(jié)構(gòu)。 
  9. > arr.pop() 
  10. 120 
  11. > arr 
  12. [ 23, 76, 98, 101 ] 
  13.  
  14. // 3.shift 從數(shù)組頭部取出元素,與push組合構(gòu)建 queue 數(shù)據(jù)結(jié)構(gòu) 
  15. > arr.shift() 
  16. 23 
  17. > arr 
  18. [ 76, 98, 101 ] 
  19.  
  20. // 4. unshift 從數(shù)組頭部添加元素 
  21. > arr.unshift(59, 145) 
  22. > arr 
  23. [ 59, 145, 76, 98, 101 ] 
  24.  
  25. // 5.splice 為特殊的方法,用于拼接數(shù)組 
  26. > arr.splice(1, 2, 55, 66, 77, 88) 
  27. [ 145, 76 ] 
  28. > arr 
  29. [ 59, 55,  66, 77, 88, 98, 101 ] 

 五、數(shù)組排序 (非純函數(shù))

最后以無處而不在的排序收尾,無論是 sort 還是 reverse 都直接在原數(shù)組上修改,也就是 inplace 操作。

1) sort 2) reverse

  1. // 1. sort  
  2. [23, 76, 98, 101].sort((x,y) => x - y) 
  3.  
  4. // 2.reverse 
  5. [23, 76, 98, 101].reverse() 

 六、思維導圖總結(jié)

 

責任編輯:姜華 來源: 今日頭條
相關推薦

2020-09-01 09:56:26

云端云計算云服務

2020-05-28 13:33:30

React Hook前端開發(fā)

2022-07-13 16:06:16

Python參數(shù)代碼

2021-02-06 11:26:55

Python開發(fā)list

2021-02-03 18:05:30

Python方法列表

2021-02-25 10:46:21

云計算云服務器云安全

2019-08-22 07:24:25

2011-06-22 15:21:08

XML

2009-03-31 13:12:30

解析XMLJava

2025-01-02 08:21:32

2021-03-10 10:13:39

爬蟲Python代碼

2018-11-01 10:10:35

網(wǎng)絡安全網(wǎng)絡攻擊網(wǎng)絡威脅

2020-01-16 18:33:24

安全數(shù)據(jù)網(wǎng)絡

2020-07-06 14:00:01

Pandas連接參數(shù)

2020-03-23 09:27:10

物聯(lián)網(wǎng)安全物聯(lián)網(wǎng)IOT

2021-03-02 13:53:50

人工智能智慧城市Infratech

2021-02-05 08:03:52

Java

2021-03-21 22:23:38

云計算數(shù)據(jù)中心IT

2010-12-01 09:04:59

PHP開發(fā)

2020-06-09 11:16:42

云計算云平臺工具
點贊
收藏

51CTO技術(shù)棧公眾號