You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
➜codecatarrayGcd.jsconstarrayGcd=arr=>{constgcd=(x,y)=>!y ? x : gcd(y,x%y);returnarr.reduce((a,b)=>gcd(a,b));}console.log(arrayGcd([1,2,3,4,5]));console.log(arrayGcd([4,8,12]));➜codenodearrayGcd.js14
➜codecatarrayLcm.jsconstarrayLcm=arr=>{constgcd=(x,y)=>(!y ? x : gcd(y,x%y));constlcm=(x,y)=>x*y/gcd(x,y);returnarr.reduce((a,b)=>lcm(a,b));};console.log(arrayLcm([1,2,3,4,5]));console.log(arrayLcm([4,8,12]));➜codenodearrayLcm.js6024
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.
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.
有意思
最近很火的
github
上的库30-seconds-of-code
,特别有意思,代码也很优雅。arrayGcd
计算数组的最大公约数。
使用
Array.reduce()
和gcd
公式(使用递归)来计算一个数组的最大公约数。gcd
即欧几里德算法,具体不表,自查。这里用到了数组的reduce方法,相当简洁,reduce不太了解的话,看下mdn就明白。arrayLcm
计算一个数组的最小公倍数。
使用
Array.reduce()
和lcm
公式(使用递归)来计算一个数组的最大公约数。lcm
算法用到了前面的gcd
算法,关键点是两个数的最大公约数和最小公倍数的乘积正好就是这两个数的乘积。arrayMax
返回数组中最大的值。
使用
Math.max()
和ES6
的扩展运算符…
返回数组中最大的值。实际上就是
Math.max()
干的事,没啥可说的了。arrayMin
返回数组中最小的值。
使用
Math.min()
和ES6
的扩展运算符…
返回数组中最小的值。实际上就是
Math.min()
干的事,没啥可说的了。chunk
按照给定的
size
将一个数组切分成含有size
个数的更小数组块的数组。使用
Array.from()
生产新的符合定义的数组。使用Array.slice()
来截取指定size
个元素组成新的数组块。如果原数组长度不能被size
整除,最后的剩余的那些元素将归属于最后一个块。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
。compact
移除掉数组里
falsey
的元素。(这个falsey
不太好翻译,不是错误的意思,而是该值布尔运算值为false
的意思,我个人常用!!
进行判断)。使用
Array.filter()
把false
、null
、0
、""
、undefined
和NaN
这些falsey
过滤掉。Array.prototype.filter()
干的,没啥好说。countOccurrences
统计一个元素在一个数组中出现的次数。
使用
Array.reduce()
在遍历过程中如果指定元素在数组中出现,则增加它的次数值,默认次数为0。三元运算符
(v === value ? a + 1 : a + 0)
遍历过程中判断遍历数组值v
是否严格等于指定值value
,是,次数a+1
;否,a+0
。最后的一个逗号后面的0,是这个初始值,即
a=0
,这个懂reduce
方法都知道,特别指出是,因为这个函数一定会有返回值,如果指定元素没有在数组中出现一次,返回值是0
,所以必须得初始化为0
。deepFlatten
深度摊平一个数组。
使用递归方法。结合
Array.concat()
、空数组[]
和ES6
的扩展运算符…
来摊平一个数组,如果摊平的元素还是一个数组,就再递归运用该方法。三元运算符
(Array.isArray(v) ? deepFlatten(v) : v)
判断v
是否是一个数组,是,返回递归运用deepFlatten(v)
后的值;否,直接返回v
。[].concat(...arr.map(fn))
用空数组把map
运算产生的数组进行…
扩展运算值拼接成结果数组返回。该方法是深度摊平方法,在很多时候还有特定的摊平一层的需求,
underscore
就有。实现的方法就是再加一个标志参数进行处理即可。具体不讲了。应该会写一个系列,今天先写到这,明天继续。
The text was updated successfully, but these errors were encountered: