偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

JavaScript30秒, 從入門到放棄

開發(fā) 前端
最近很火的github上的庫30-seconds-of-code ,特別有意思,代碼也很優(yōu)雅。能學(xué)es6、自己翻譯,能學(xué)英語、代碼很美,很優(yōu)雅,美即正義、函數(shù)式表達(dá),享受等。

有意思

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

[[214475]]

  1. 能學(xué)es6
  2. 自己翻譯,能學(xué)英語
  3. 代碼很美,很優(yōu)雅,美即正義
  4. 函數(shù)式表達(dá),享受

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 

計(jì)算數(shù)組的***公約數(shù)。

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

 

  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 即歐幾里德算法,具體不表,自查。這里用到了數(shù)組的reduce方法,相當(dāng)簡潔,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 

計(jì)算一個數(shù)組的最小公倍數(shù)。

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

 

  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 算法,關(guān)鍵點(diǎn)是兩個數(shù)的***公約數(shù)和最小公倍數(shù)的乘積正好就是這兩個數(shù)的乘積。

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 

返回?cái)?shù)組中***的值。

使用 Math.max() 和 ES6 的擴(kuò)展運(yùn)算符 … 返回?cái)?shù)組中***的值。

 

  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 

實(shí)際上就是 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 

返回?cái)?shù)組中最小的值。

使用 Math.min() 和 ES6 的擴(kuò)展運(yùn)算符 … 返回?cái)?shù)組中最小的值。

 

  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 

實(shí)際上就是 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 將一個數(shù)組切分成含有 size 個數(shù)的更小數(shù)組塊的數(shù)組。

使用 Array.from() 生產(chǎn)新的符合定義的數(shù)組。使用 Array.slice() 來截取指定 size 個元素組成新的數(shù)組塊。如果原數(shù)組長度不能被 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) 這個方法呢,***個參數(shù)是一個類數(shù)組或者可迭代的對象,第二個參數(shù)是一個應(yīng)用在每一個數(shù)組元素上的方法,第三個參數(shù)就是改變 this 的指向了。通俗說就是指定誰是你的爸爸。

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

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

arr.slice(i * size, i * size + size) 迭代過程中每次截取 size 個數(shù)的元素組成新數(shù)組。這里的 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, 

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

使用 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 

統(tǒng)計(jì)一個元素在一個數(shù)組中出現(xiàn)的次數(shù)。

使用 Array.reduce() 在遍歷過程中如果指定元素在數(shù)組中出現(xiàn),則增加它的次數(shù)值,默認(rèn)次數(shù)為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 

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

***的一個逗號后面的0,是這個初始值,即 a=0 ,這個懂 reduce 方法都知道,特別指出是,因?yàn)檫@個函數(shù)一定會有返回值,如果指定元素沒有在數(shù)組中出現(xiàn)一次,返回值是 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] 

深度攤平一個數(shù)組。

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

 

  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 ] 

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

[].concat(...arr.map(fn)) 用空數(shù)組把 map 運(yùn)算產(chǎn)生的數(shù)組進(jìn)行 … 擴(kuò)展運(yùn)算值拼接成結(jié)果數(shù)組返回。

該方法是深度攤平方法,在很多時候還有特定的攤平一層的需求, underscore 就有。實(shí)現(xiàn)的方法就是再加一個標(biāo)志參數(shù)進(jìn)行處理即可。具體不講了。

應(yīng)該會寫一個系列,今天先寫到這,明天繼續(xù)。

責(zé)任編輯:未麗燕 來源: SegmentFault
相關(guān)推薦

2016-08-03 16:01:47

GitLinux開源

2019-07-02 14:17:18

API網(wǎng)關(guān)網(wǎng)關(guān)流量

2017-03-25 20:30:15

2020-07-07 10:50:19

Python丄則表達(dá)文本

2025-04-22 02:00:00

芯片晶圓光刻機(jī)

2022-01-17 08:52:32

CPUCPU工具顯卡

2022-03-28 11:00:34

JVMJava對象

2021-11-08 07:11:49

決策樹數(shù)據(jù)分類器

2019-08-21 14:35:18

壓縮文件優(yōu)化過程Java

2025-06-27 09:05:47

2022-04-19 11:25:31

JVMZGC垃圾收集器

2020-04-10 15:05:09

深度學(xué)習(xí)人工智能蒸餾

2018-01-26 14:35:16

程序員入門經(jīng)歷

2021-05-11 11:08:37

電腦病毒軟件

2021-08-02 06:49:46

Flutter Router安全

2019-06-23 15:21:42

Google谷歌平板

2020-11-12 18:51:43

Java編程語言

2022-09-30 15:46:26

Babel編譯器插件

2025-06-25 09:30:14

2021-10-25 05:54:59

SSD固態(tài)硬盤存儲
點(diǎn)贊
收藏

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