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

JavaScript30秒, 從入門到放棄

開發 前端
最近很火的github上的庫30-seconds-of-code ,特別有意思,代碼也很優雅。能學es6、自己翻譯,能學英語、代碼很美,很優雅,美即正義、函數式表達,享受等。

有意思

最近很火的 github 上的庫 30-seconds-of-code ,特別有意思,代碼也很優雅。

[[214475]]

  1. 能學es6
  2. 自己翻譯,能學英語
  3. 代碼很美,很優雅,美即正義
  4. 函數式表達,享受

arrayGcd

Calculates the greatest common denominator (gcd) of an array of numbers.

Use Array.reduce() and the gcd formula (uses recursion) to calculate the greatest common denominator of an array of numbers.

 

  1. const arrayGcd = arr =>{ 
  2.   const gcd = (x, y) => !y ? x : gcd(y, x % y); 
  3.   return arr.reduce((a,b) => gcd(a,b)); 
  4. // arrayGcd([1,2,3,4,5]) -> 1 
  5. // arrayGcd([4,8,12]) -> 4 

計算數組的***公約數。

使用 Array.reduce() 和 gcd 公式(使用遞歸)來計算一個數組的***公約數。

 

  1. ➜  code cat arrayGcd.js 
  2. const arrayGcd = arr => { 
  3.     const gcd = (x, y) => !y ? x : gcd(y, x % y); 
  4.     return arr.reduce((a, b) => gcd(a, b)); 
  5.  
  6. console.log(arrayGcd([1, 2, 3, 4, 5])); 
  7. console.log(arrayGcd([4, 8, 12])); 
  8. ➜  code node arrayGcd.js 

gcd 即歐幾里德算法,具體不表,自查。這里用到了數組的reduce方法,相當簡潔,reduce不太了解的話,看下 mdn 就明白。

arrayLcm

Calculates the lowest common multiple (lcm) of an array of numbers.

Use Array.reduce() and the lcm formula (uses recursion) to calculate the lowest common multiple of an array of numbers.

 

  1. const arrayLcm = arr =>{ 
  2.  const gcd = (x, y) => !y ? x : gcd(y, x % y); 
  3.  const lcm = (x, y) => (x*y)/gcd(x, y)  
  4.  return arr.reduce((a,b) => lcm(a,b)); 
  5. // arrayLcm([1,2,3,4,5]) -> 60 
  6. // arrayLcm([4,8,12]) -> 24 

計算一個數組的最小公倍數。

使用 Array.reduce() 和 lcm 公式(使用遞歸)來計算一個數組的***公約數。

 

  1. ➜  code cat arrayLcm.js 
  2. const arrayLcm = arr => { 
  3.   const gcd = (x, y) => (!y ? x : gcd(y, x % y)); 
  4.   const lcm = (x, y) => x * y / gcd(x, y); 
  5.   return arr.reduce((a, b) => lcm(a, b)); 
  6. }; 
  7.  
  8. console.log(arrayLcm([1, 2, 3, 4, 5])); 
  9. console.log(arrayLcm([4, 8, 12])); 
  10. ➜  code node arrayLcm.js 
  11. 60 
  12. 24 

lcm 算法用到了前面的 gcd 算法,關鍵點是兩個數的***公約數和最小公倍數的乘積正好就是這兩個數的乘積。

arrayMax

Returns the maximum value in an array.

Use Math.max() combined with the spread operator ( ... ) to get the maximum value in the array.

 

  1. const arrayMax = arr => Math.max(...arr); 
  2. // arrayMax([10, 1, 5]) -> 10 

返回數組中***的值。

使用 Math.max() 和 ES6 的擴展運算符 … 返回數組中***的值。

 

  1. ➜  code cat arrayMax.js 
  2. const arrayMax = arr => Math.max(...arr); 
  3.  
  4. console.log(arrayMax([10, 1, 5])); 
  5. ➜  code node arrayMax.js 
  6. 10 

實際上就是 Math.max() 干的事,沒啥可說的了。

arrayMin

Returns the minimum value in an array.

Use Math.min() combined with the spread operator ( ... ) to get the minimum value in the array.

 

  1. const arrayMin = arr => Math.min(...arr); 
  2. // arrayMin([10, 1, 5]) -> 1 

返回數組中最小的值。

使用 Math.min() 和 ES6 的擴展運算符 … 返回數組中最小的值。

 

  1. ➜  code cat arrayMin.js 
  2. const arrayMin = arr => Math.min(...arr); 
  3.  
  4. console.log(arrayMin([10, 1, 5])); 
  5. ➜  code node arrayMin.js 

實際上就是 Math.min() 干的事,沒啥可說的了。

chunk

Chunks an array into smaller arrays of a specified size.

Use Array.from() to create a new array, that fits the number of chunks that will be produced. Use Array.slice() to map each element of the new array to a chunk the length of size . If the original array can't be split evenly, the final chunk will contain the remaining elements.

 

  1. const chunk = (arr, size) => 
  2.  Array.from({length: Math.ceil(arr.length / size)}, (v, i) => arr.slice(i * size, i * size + size)); 
  3. // chunk([1,2,3,4,5], 2) -> [[1,2],[3,4],[5]] 

按照給定的 size 將一個數組切分成含有 size 個數的更小數組塊的數組。

使用 Array.from() 生產新的符合定義的數組。使用 Array.slice() 來截取指定 size 個元素組成新的數組塊。如果原數組長度不能被 size 整除,***的剩余的那些元素將歸屬于***一個塊。

 

  1. ➜  code cat chunk.js 
  2. const chunk = (arr, size) => 
  3.   Array.from({ length: Math.ceil(arr.length / size) }, (v, i) => 
  4.     arr.slice(i * size, i * size + size
  5.   ); 
  6.  
  7. console.log(chunk([1, 2, 3, 4, 5], 2)); 
  8. ➜  code node chunk.js 
  9. [ [ 1, 2 ], [ 3, 4 ], [ 5 ] ] 

Array.from(arrayLike, mapFn, thisArg) 這個方法呢,***個參數是一個類數組或者可迭代的對象,第二個參數是一個應用在每一個數組元素上的方法,第三個參數就是改變 this 的指向了。通俗說就是指定誰是你的爸爸。

這里用了一個 { length: Math.ceil(arr.length / size) } 迭代對象, length 指定了迭代次數,即按照 size 分塊后的數組長度,正好就是原數組長度除以 size 向上取整的值。向上取整就是為了滿足不能完全整除的情況。比如5個元素按照2個一組進行分塊,分了兩組兩個元素的,剩***一個元素成了獨立組,總長為3。

(v, i) ,由于迭代的時候數組在每一個位置上都是以 undefined 初始化的,所以 v 一直都是 undefined 。

arr.slice(i * size, i * size + size) 迭代過程中每次截取 size 個數的元素組成新數組。這里的 i 就是隨著迭代變化,比如 length 是3, i 就是0,1,2。

這里的迭代類似 python 里的 range 。

 

  1. ➜  code python 
  2. Python 3.6.4 (defaultDec 23 2017, 10:37:40) 
  3. [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin 
  4. Type "help""copyright""credits" or "license" for more information. 
  5. >>> import math 
  6. >>> arr = [1,2,3,4,5] 
  7. >>> size = 2 
  8. >>> for i in range(math.ceil(len(arr) / size)): 
  9. ...     print('index: ', i) 
  10. ... 
  11. index:  0 
  12. index:  1 
  13. index:  2 

compact

Removes falsey values from an array.

Use Array.filter() to filter out falsey values ( false , null , 0 , "" , undefined , and NaN ).

 

  1. const compact = arr => arr.filter(Boolean); 
  2. // compact([0, 1, false, 2, '', 3, 'a''e'*23, NaN, 's', 34]) -> [ 1, 2, 3, 

移除掉數組里 falsey 的元素。(這個 falsey 不太好翻譯,不是錯誤的意思,而是該值布爾運算值為 false 的意思,我個人常用 !! 進行判斷)。

使用 Array.filter() 把 false 、 null 、 0 、 "" 、 undefined 和 NaN 這些 falsey 過濾掉。

 

  1. ➜  code cat compact.js 
  2. const compact = arr => arr.filter(Boolean); 
  3.  
  4. console.log(compact([0, 1, false, 2, "", 3, "a""e" * 23, NaN, "s", 34])); 
  5. ➜  code node compact.js 
  6. [ 1, 2, 3, 'a''s', 34 ] 

Array.prototype.filter() 干的,沒啥好說。

countOccurrences

Counts the occurrences of a value in an array.

Use Array.reduce() to increment a counter each time you encounter the specific value inside the array.

 

  1. const countOccurrences = (arr, value) => arr.reduce((a, v) => v === value ? a + 1 : a + 0, 0); 
  2. // countOccurrences([1,1,2,1,2,3], 1) -> 3 

統計一個元素在一個數組中出現的次數。

使用 Array.reduce() 在遍歷過程中如果指定元素在數組中出現,則增加它的次數值,默認次數為0。

 

  1. ➜  code cat countOccurrences.js 
  2. const countOccurrences = (arr, value) => 
  3.   arr.reduce((a, v) => (v === value ? a + 1 : a + 0), 0); 
  4.  
  5. console.log(countOccurrences([1, 1, 2, 1, 2, 3], 1)); 
  6. console.log(countOccurrences([1, 1, 2, 1, 2, 3], 5)); 
  7. ➜  code node countOccurrences.js 

三元運算符 (v === value ? a + 1 : a + 0) 遍歷過程中判斷遍歷數組值 v 是否嚴格等于指定值 value ,是,次數 a+1 ;否, a+0 。

***的一個逗號后面的0,是這個初始值,即 a=0 ,這個懂 reduce 方法都知道,特別指出是,因為這個函數一定會有返回值,如果指定元素沒有在數組中出現一次,返回值是 0 ,所以必須得初始化為 0 。

deepFlatten

Deep flattens an array.

Use recursion. Use Array.concat() with an empty array ( [] ) and the spread operator ( ... ) to flatten an array. Recursively flatten each element that is an array.

 

  1. const deepFlatten = arr => [].concat(...arr.map(v => Array.isArray(v) ? deepFlatten(v) : v)); 
  2. // deepFlatten([1,[2],[[3],4],5]) -> [1,2,3,4,5] 

深度攤平一個數組。

使用遞歸方法。結合 Array.concat() 、空數組 [] 和 ES6 的擴展運算符 … 來攤平一個數組,如果攤平的元素還是一個數組,就再遞歸運用該方法。

 

  1. ➜  code cat deepFlatten.js 
  2. const deepFlatten = arr => 
  3.   [].concat(...arr.map(v => (Array.isArray(v) ? deepFlatten(v) : v))); 
  4.  
  5. console.log(deepFlatten([1, [2], [[3], 4], 5])); 
  6. ➜  code node deepFlatten.js 
  7. [ 1, 2, 3, 4, 5 ] 

三元運算符 (Array.isArray(v) ? deepFlatten(v) : v) 判斷 v 是否是一個數組,是,返回遞歸運用 deepFlatten(v) 后的值;否,直接返回 v 。

[].concat(...arr.map(fn)) 用空數組把 map 運算產生的數組進行 … 擴展運算值拼接成結果數組返回。

該方法是深度攤平方法,在很多時候還有特定的攤平一層的需求, underscore 就有。實現的方法就是再加一個標志參數進行處理即可。具體不講了。

應該會寫一個系列,今天先寫到這,明天繼續。

責任編輯:未麗燕 來源: SegmentFault
相關推薦

2016-08-03 16:01:47

GitLinux開源

2019-07-02 14:17:18

API網關網關流量

2017-03-25 20:30:15

2020-07-07 10:50:19

Python丄則表達文本

2025-04-22 02:00:00

芯片晶圓光刻機

2021-11-08 07:11:49

決策樹數據分類器

2022-03-28 11:00:34

JVMJava對象

2022-01-17 08:52:32

CPUCPU工具顯卡

2025-06-27 09:05:47

2019-08-21 14:35:18

壓縮文件優化過程Java

2018-01-26 14:35:16

程序員入門經歷

2019-06-23 15:21:42

Google谷歌平板

2021-05-11 11:08:37

電腦病毒軟件

2021-08-02 06:49:46

Flutter Router安全

2022-04-19 11:25:31

JVMZGC垃圾收集器

2020-04-10 15:05:09

深度學習人工智能蒸餾

2020-11-12 18:51:43

Java編程語言

2022-09-30 15:46:26

Babel編譯器插件

2025-06-25 09:30:14

2021-02-06 22:10:12

宏定義處理器代碼
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 99久久夜色精品国产亚洲96 | 久久久一二三区 | 观看av| 久久久成人一区二区免费影院 | 色吧久久 | 在线免费视频一区 | 日韩成人在线免费视频 | 精品欧美乱码久久久久久 | 欧美国产日本一区 | 国产一区二区三区色淫影院 | 精品成人av | 亚洲精品一区二区三区四区高清 | av中文在线 | 久久国产综合 | 一级做a爰片久久毛片免费看 | 99精品国产成人一区二区 | 免费在线视频一区二区 | 女朋友的闺蜜3韩国三级 | 国产免费又黄又爽又刺激蜜月al | 亚洲不卡在线观看 | 亚洲一区二区视频 | 日韩一级欧美一级 | 国产精品成人在线 | 国产一区二区三区视频 | 一区二区三区四区国产 | 欧美a v在线 | 国产精品久久久久久网站 | 国产精品99免费视频 | 一区二区视频 | 成人在线亚洲 | 国产一二三区电影 | 天堂一区在线 | 午夜小视频在线观看 | 国产激情精品一区二区三区 | 久久国产日本 | 久久精品国产久精国产 | 精品久久ai电影 | 久久综合一区 | 久久久久国产一区二区三区四区 | 在线视频国产一区 | 一区二区三区在线看 |