30 seconds of code js javascript
【摘要】
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)...
-
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) =>
-
arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => {
-
acc[val] = (acc[val] || 0) + 1;
-
return acc;
-
}, {});
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)));
-
const difference = (a, b) => {
-
const s = new Set(b);
-
return a.filter(x => !s.has(x));
-
};
-
const differenceBy = (a, b, fn) => {
-
const s = new Set(b.map(fn));
-
return a.map(fn).filter(el => !s.has(el));
-
};
const differenceWith = (arr, val, comp) => arr.filter(a => val.findIndex(b => comp(a, b)) === -1);
const dropRight = (arr, n = 1) => arr.slice(0, -n);
-
const dropRightWhile = (arr, func) => {
-
while (arr.length > 0 && !func(arr[arr.length - 1])) arr = arr.slice(0, -1);
-
return arr;
-
};
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)