30 seconds of code js javascript

举报
拿我格子衫来 发表于 2022/02/27 00:41:21 2022/02/27
【摘要】 const chunk = (arr, size) => Array.from({ length: Math.ceil(arr.length / size) }, (v, i) => arr.slice(i * size, i * size + size) ); const countBy = (arr, fn)...

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

  
  1. const countBy = (arr, fn) =>
  2. arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => {
  3. acc[val] = (acc[val] || 0) + 1;
  4. return acc;
  5. }, {});
const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);
 
const deepFlatten = arr => [].concat(...arr.map(v => (Array.isArray(v) ? deepFlatten(v) : v)));
 

  
  1. const difference = (a, b) => {
  2. const s = new Set(b);
  3. return a.filter(x => !s.has(x));
  4. };

  
  1. const differenceBy = (a, b, fn) => {
  2. const s = new Set(b.map(fn));
  3. return a.map(fn).filter(el => !s.has(el));
  4. };
const differenceWith = (arr, val, comp) => arr.filter(a => val.findIndex(b => comp(a, b)) === -1);
 
const dropRight = (arr, n = 1) => arr.slice(0, -n);
 

  
  1. const dropRightWhile = (arr, func) => {
  2. while (arr.length > 0 && !func(arr[arr.length - 1])) arr = arr.slice(0, -1);
  3. return arr;
  4. };
const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
 

 

文章来源: fizzz.blog.csdn.net,作者:拿我格子衫来,版权归原作者所有,如需转载,请联系作者。

原文链接:fizzz.blog.csdn.net/article/details/90041287

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。