参考:https://1loc.dev/
将一个值转为数组的方法
| const castArray = (value) => (Array.isArray(value) ? value : [value]); |
| |
| caseArray(1); |
| |
检查数组是否为空数组
| const isEmpty = (arr) => Array.isArray(arr) && !arr.length; |
| |
| isEmpty([]); |
| isEmpty([1, 2, 3]); |
不用考虑两个数组的顺序直接比较两个数组是否相同
| |
| const isEqual = (a, b) => JSON.stringify([...new Set(a)].sort()) === JSON.stringify([...new Set(b)].sort()); |
| |
| isEqual([1, 2, 3], [1, 2, 3]); |
| isEqual([], [1, 2]); |
数组拷贝方式
| const res = (arr) => arr.slice(0); |
| |
| const res = (arr) => [...arr]; |
| |
| const res = (arr) => Array.from(arr); |
将一个对象数组转为一个单一对象
| const toObject = (arr, key) => arr.reduce((a, b) => ({ ...a, [b[key]]: b }), {}); |
| |
| const toObject = (arr, key) => Object.fromEntries(arr.map((it) => [it[key], it])); |
将一组字符串转为真正数组
| const toNumbers = (arr) => arr.map(Number); |
| |
| const toNumbers = (arr) => arr.map((x) => +x); |
| |
| toNumbers(['2', '3', '4']); |
针对对象数组,根据某个字段统计相同值的个数
| const countBy = (arr, prop) => arr.reduce((prev, curr) => ((prev[curr[prop]] = ++prev[curr[prop]] || 1), prev), {}); |
| |
| countBy( |
| [ |
| { branch: 'audi', model: 'q8', year: '2019' }, |
| { branch: 'audi', model: 'rs7', year: '2020' }, |
| { branch: 'ford', model: 'mustang', year: '2019' }, |
| { branch: 'ford', model: 'explorer', year: '2020' }, |
| { branch: 'bmw', model: 'x7', year: '2020' }, |
| ], |
| 'branch' |
| ); |
| |
| |
| |
统计一个数组中某个值出现的次数
| const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0); |
| |
| countOccurrences([2, 1, 3, 3, 2, 3], 2); |
| countOccurrences(['a', 'b', 'a', 'c', 'a', 'b'], 'a'); |
比较两个数组包含的内容是否相同
| const isEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b); |
| |
| isEqual([1, 2, 3], [1, 2, 3]); |
| isEqual([1, 2, 3], [1, '2', 3]); |
统计一个数组中相同值出现的次数
| const countOccurrences = (arr) => arr.reduce((prev, curr) => ((prev[curr] = ++prev[curr] || 1), prev), {}); |
| |
| countOccurrences([2, 1, 3, 3, 2, 3]); // { '1': 1, '2': 2, '3': 3 } |
| countOccurrences(['a', 'b', 'a', 'c', 'a', 'b']); // { 'a': 3, 'b': 2, 'c': 1 } |
寻找数组中最小值所在的索引位置
| const indexOfMin = (arr) => arr.reduce((prev, curr, i, a) => (curr < a[prev] ? i : prev), 0); |
| |
| indexOfMin([6, 4, 8, 2, 10]); |
| indexOfMin([6, 4, 2, 2, 10]); |
数组对象中取得数组对象中其中一个字段的值最大的那个数组
| const maxBy = (arr, key) => arr.reduce((a, b) => (a[key] >= b[key] ? a : b), {}); |
| const people = [ |
| { name: 'Bar', age: 24 }, |
| { name: 'Baz', age: 32 }, |
| { name: 'Foo', age: 42 }, |
| { name: 'Fuzz', age: 36 }, |
| ]; |
| maxBy(people, 'age'); |
数组对象中取得数组对象中其中一个字段的值最小的那个数组
| const minBy = (arr, key) => arr.reduce((a, b) => (a[key] < b[key] ? a : b), {}); |
| |
| const people = [ |
| { name: 'Bar', age: 24 }, |
| { name: 'Baz', age: 32 }, |
| { name: 'Foo', age: 42 }, |
| { name: 'Fuzz', age: 36 }, |
| ]; |
| minBy(people, 'age'); |
数组扁平化
| |
| const flat = (arr) => arr.reduce((a, b) => (Array.isArray(b) ? [...a, ...flat(b)] : [...a, b]), []); |
| |
| |
| |
| const flat = (arr) => arr.flat(); |
| |
| flat(['cat', ['lion', 'tiger']]); |
获取一个数组中所有值的子集
| const getSubsets = (arr) => arr.reduce((prev, curr) => prev.concat(prev.map((k) => k.concat(curr))), [[]]); |
| getSubsets([1, 2]); // [[], [1], [2], [1, 2]] |
| getSubsets([1, 2, 3]); // [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]] |
获取数组的平均值
| const average = (arr) => arr.reduce((a, b) => a + b, 0) / arr.length; |
获取两个数组之间的交集
| const getIntersection = (a, ...arr) => [...new Set(a)].filter((v) => arr.every((b) => b.includes(v))); |
| |
| getIntersection([1, 2, 3], [2, 3, 4, 5]); |
| getIntersection([1, 2, 3], [2, 3, 4, 5], [1, 3, 5]); |
数组去重(得到非重复数组)
| const unique = (arr) => [...new Set(arr)]; |
| |
| |
| const unique = (arr) => arr.filter((el, i, array) => array.indexOf(el) === i); |
数组分组处理(根据某个key对数组进行分组处理)
| const groupBy = (arr, key) => arr.reduce((acc, item) => ((acc[item[key]] = [...(acc[item[key]] || []), item]), acc), {}); |
| |
| groupBy( |
| [ |
| { branch: 'audi', model: 'q8', year: '2019' }, |
| { branch: 'audi', model: 'rs7', year: '2020' }, |
| { branch: 'ford', model: 'mustang', year: '2019' }, |
| { branch: 'ford', model: 'explorer', year: '2020' }, |
| { branch: 'bmw', model: 'x7', year: '2020' }, |
| ], |
| 'branch' |
| ); |
| |
| /* |
| { |
| audi: [ |
| { branch: 'audi', model: 'q8', year: '2019' }, |
| { branch: 'audi', model: 'rs7', year: '2020' } |
| ], |
| bmw: [ |
| { branch: 'bmw', model: 'x7', year: '2020' } |
| ], |
| ford: [ |
| { branch: 'ford', model: 'mustang', year: '2019' }, |
| { branch: 'ford', model: 'explorer', year: '2020' } |
| ], |
| } |
| */ |
移除数组中重复的值
| const removeDuplicate = (arr) => arr.filter((i) => arr.indexOf(i) === arr.lastIndexOf(i)); |
| |
| removeDuplicate(['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']); // ['h', 'e', 'w', 'r', 'd'] |
合并并对数组去重
| const union = (...arr) => [...new Set(arr.flat())]; |
| |
| union([1, 2], [2, 3], [3]); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
2022-02-09 加载额外配置文件的方式