🎶 Sym - 一款用 Java 实现的现代化社区(论坛/BBS/社交网络/博客)平台

📕 思源笔记 - 一款桌面端笔记应用,支持 Windows、Mac 和 Linux

🎸 Solo - B3log 分布式社区的博客端节点,欢迎加入下一代社区网络

♏ Vditor - 一款浏览器端的 Markdown 编辑器

0 篇文章

Array - sortedIndexBy

2019-06-09 描述基于提供的迭代方法,根据数组原有的排序规则把对象插入最接近且索引值最小的正确位置后返回该索引值。提示宽松的检查数组的排序规则是否为降序使用 Array.prototype.findIndex()找出元素应该插入的最接近的索引所有比较需基于迭代函数 fn 代码 constsortedIndexBy=(arr,n,fn)=>{constisDescending=fn(arr[0])>fn

Array - sortedIndex

2019-06-08 描述把一个元素按照数组中原有的排序规则插入该数组中适当的位置,并返回插入位置的最小索引值。提示宽松的检查数组的排序规则是否为降序使用 Array.prototype.findIndex()找出元素应该插入的最接近的索引代码 constsortedIndex=(arr,n)=>{constisDescending=arr[0]>arr[arr.length-1];constindex=

Array - similarity

2019-06-07 描述返回一个数组,该数组中包含两个数组中相似的元素。提示使用 Array.prototype.filter()过滤出那些值不在另一个数组中的元素使用 Array.prototype.includes()来判断另一个数组中是否包含该值代码 constsimilarity=(arr,values)=>arr.filter(v=>values.includes(v));示例找出两个数组中相同

Array - shuffle

2019-06-06 描述对数组中的值进行随机排序后并返回新生成的数组。提示使用 Fisher-Yates(洗牌)算法将数组进行随机排列使用扩展运算符...创建新的数组代码 constshuffle=([...arr])=>{letm=arr.length;while(m){consti=Math.floor(Math.random()*m--);[arr[m],arr[i]]=[arr[i],arr[m

Array - shank

2019-06-05 描述和 Array.prototype.splice()有着同样的功能,但是需要返回新数组来替代对原始数组的修改。提示使用 Array.prototype.slice()和 Array.prototype.concat()来获取移除存在的元素并且(或者)添加新元素后的新数组内容。第二个参数为 index,默认从 0 开始第四个元素为 elements,默认不添加新元素代码 constshank=

Array - sampleSize

2019-06-04 描述从键值唯一的数组中获取 n 个随机元素。提示使用 Fisher-Yates(洗牌)算法将数组进行随机排列使用 Array.prototype.slice()获取前 n 个元素第二个参数的默认值为 1 代码 constsampleSize=([...arr],n=1)=>{letm=arr.length;while(m){consti=Math.floor(Math.random()*m--);

Array - sample

2019-06-03 描述从数组中返回一个随机元素。提示使用 Math.random()和数组的长度相乘来生成随机数使用 Math.floor()进行四舍五入,从而使结果为最接近的整数这个方法同样适用于字符串代码 constsample=arr=>arr[Math.floor(Math.random()*arr.length)];示例随机返回数组中的一个元素:sample([3,7,9,11]);返回总目录

Array - remove

2019-06-02 描述从数组中移除给定函数返回 false 的元素。提示使用 Array.prototype.filter()找出数组中返回真值的元素在 Array.prototype.reduce()中使用 Array.prototype.splice()来移除原始数组中的元素调用的 func 可以依此传入三个参数(value,index,array)代码 constremove=(arr,func)=>Arr

Array - reject

2019-06-01 描述返回数组中不满足条件的元素。提示使用 Array.prototype.filter()过滤出不满足条件的元素代码 constreject=(pred,array)=>array.filter((...args)=>!pred(...args));示例返回奇数:reject(x=>x%2===0,[1,2,3,4,5]);//[1,3,5]返回长度小于 5 的单词:reject(wor