# 数组
# 生成数组
当你需要要生成一个 0-99 的数组
方案 1
| const createArr = (n) => Array.from(new Array(n), (v, i) => i) |
| const arr = createArr(100) |
方案 2
| const createArr = (n) => new Array(n).fill(0).map((v, i) => i) |
| createArr(100) |
# 打乱数组
当你有一个数组,你需要打乱这个数组的排序
| const randomSort = list => list.sort(() => Math.random() - 0.5) |
| randomSort([0,1,2,3,4,5,6,7,8,9]) |
# 数组去重
当你需要将数组中的所有重复的元素只保留一个
方案 1
| const removeDuplicates = list => [...new Set(list)] |
| removeDuplicates([0, 0, 2, 4, 5]) |
方案 2
| const removeDuplicates = list => Array.from(new Set(list)) |
| removeDuplicates([0, 0, 2, 4, 5]) |
# 多数组取交集
当你需要取多个数组中的交集
| const intersection = (a, ...arr) => [...new Set(a)].filter((v) => arr.every((b) => b.includes(v))) |
| |
| intersection([1, 2, 3, 4], [2, 3, 4, 7, 8], [1, 3, 4, 9]) |
| |
# 查找最大值索引
但你需要找到一个数组中的最大值的索引
| const indexOfMax = (arr) => arr.reduce((prev, curr, i, a) => (curr > a[prev] ? i : prev), 0); |
| indexOfMax([1, 3, 9, 7, 5]); |
# 查找最小值索引
当你需要找到一个数组中的最小值的索引
| const indexOfMin = (arr) => arr.reduce((prev, curr, i, a) => (curr < a[prev] ? i : prev), 0) |
| indexOfMin([2, 5, 3, 4, 1, 0, 9]) |
# 找到最接近的数值
当你需要在一个数组中找到一个最接近的值
| const closest = (arr, n) => arr.reduce((prev, curr) => (Math.abs(curr - n) < Math.abs(prev - n) ? curr : prev)) |
| closest([29, 87, 8, 78, 97, 20, 75, 33, 24, 17], 50) |
# 压缩多个数组
当你需要将多个数组压缩成一个数组
| const zip = (...arr) => Array.from({ length: Math.max(...arr.map((a) => a.length)) }, (_, i) => arr.map((a) => a[i])) |
| zip([1,2,3,4], ['a', 'b', 'c', 'd'], ['A', 'B', 'C', 'D']) |
| |
# 矩阵交换行和列
当你需要将一个矩阵的行和列进行互相交换
| const transpose = (matrix) => matrix[0].map((col, i) => matrix.map((row) => row[i])); |
| transpose( |
| [ |
| [1, 2, 3], |
| [4, 5, 6], |
| [7, 8, 9], |
| ] |
| ); |
# 数字转换
# 进制转换
将 10 进制转换成 n 进制,可以使用 toString (n)
| const toDecimal = (num, n = 10) => num.toString(n) |
| |
| toDecimal(10, 2) |
将 n 进制转换成 10 进制,可以使用 parseInt (num, n)
| |
| const toDecimalism = (num, n = 10) => parseInt(num, n) |
| toDecimalism(1010, 2) |
# 正则
# 手机号格式化
当你需要将手机号码格式化成 xxx-xxxx-xxxx 的形式
| const formatPhone = (str, sign = '-') => str.replace(/(\W|\s)/g, "").split(/^(\d{3})(\d{4})(\d{4})$/).filter(item => item).join(sign) |
| |
| formatPhone('13123456789') |
| formatPhone('13 1234 56 789', ' ') |
# 去除多余空格
当你需要将一段文本中的多个空格合并成一个空格
| const setTrimOut = str => str.replace(/\s\s+/g, ' ') |
| const str = setTrimOut('hello, jack') |
# web
# 重新加载当前页面
| const reload = () => location.reload(); |
| reload() |
# 滚动到页面顶部
如果你需要将页面翻到最顶部
| const goToTop = () => window.scrollTo(0, 0); |
| goToTop() |
# 元素滚动
如果你希望将一个元素顺滑的滚动到可视区域的起点
| const scrollToTop = (element) => |
| element.scrollIntoView({ behavior: "smooth", block: "start" }) |
| scrollToTop(document.body) |
如果你希望将一个元素顺滑的滚动到可视区域的终点
| const scrollToBottom = (element) => |
| element.scrollIntoView({ behavior: "smooth", block: "end" }) |
| scrollToBottom(document.body) |
# 检查当前是否 IE 浏览器
| const isIE = !!document.documentMode; |
# 从给定文本中剥离 html
当你需要在某个文本中将里面的标签全部过滤掉
| const stripHtml = (html) => new DOMParser().parseFromString(html, 'text/html').body.textContent || ''; |
| stripHtml('<div>test</div>') |
# 重定向
当你需要跳转到其他页面
| const goTo = (url) => (location.href = url); |
# 文本粘贴
当你需要复制文本到粘贴板上
| const copy = (text) => navigator.clipboard?.writeText && navigator.clipboard.writeText(text) |
| copy('你需要粘贴的文本') |
# 日期
# 判断日期是否为今天
| const isToday = (date) => date.toISOString().slice(0, 10) === new Date().toISOString().slice(0, 10) |
# 日期转换
当你需要将日期转换为为 YYYY-MM-DD 格式
| const formatYmd = (date) => date.toISOString().slice(0, 10); |
| formatYmd(new Date()) |
# 秒数转换
当你需要将秒数转换为 hh:mm:ss 格式
| const formatSeconds = (s) => new Date(s * 1000).toISOString().substr(11, 8) |
| formatSeconds(200) |
# 获取某年某月的第一天
当你需要获取某年某月的第一天
| const getFirstDate = (d = new Date()) => new Date(d.getFullYear(), d.getMonth(), 1); |
| getFirstDate(new Date('2022-04')) |
# 获取某年某月的最后一天
当你需要获取某年某月的最后一天
| const getLastDate = (d = new Date()) => new Date(d.getFullYear(), d.getMonth() + 1, 0); |
| getLastDate(new Date('2023-03-04')) |
# 获取某年月份天数
当你需要获取某年某个月份的总天数
| const getDaysNum = (year, month) => new Date(year, month, 0).getDate() |
| const day = getDaysNum(2024, 2) |
# 函数
# 异步函数判断
判断一个函数是否属于异步函数
| const isAsyncFunction = (v) => Object.prototype.toString.call(v) === '[object AsyncFunction]' |
| isAsyncFunction(async function () {}); |
# 数字
# 截断数字
当你需要将小数点后的某些数字截断而不取四舍五入
| const toFixed = (n, fixed) => `${n}`.match(new RegExp(`^-?\d+(?:.\d{0,${fixed}})?`))[0] |
| toFixed(10.255, 2) |
# 四舍五入
当你需要将小数点后的某些数字截断,并取四舍五入
| const round = (n, decimals = 0) => Number(`${Math.round(`${n}e${decimals}`)}e-${decimals}`) |
| round(10.255, 2) |
# 补零
当你需要在一个数字 num 不足 len 位数的时候前面补零操作
| const replenishZero = (num, len, zero = 0) => num.toString().padStart(len, zero) |
| replenishZero(8, 2) |
# 对象
# 删除无效属性
当你需要删除一个对象中的属性值为 null 或 undefined 的所有属性
| const removeNullUndefined = (obj) => Object.entries(obj).reduce((a, [k, v]) => (v == null ? a : ((a[k] = v), a)), {}); |
| |
| removeNullUndefined({name: '', age: undefined, sex: null}) |
# 反转对象键值
当你需要将对象的键值对交换
| const invert = (obj) => Object.keys(obj).reduce((res, k) => Object.assign(res, { [obj[k]]: k }), {}) |
| invert({name: 'jack'}) |
# 字符串转对象
当你需要将一串字符串比如 '{name: "jack"}' 转换成对象时,直接使用 JSON.parse 将会报错。
| const strParse = (str) => JSON.parse(str.replace(/(\w+)\s*:/g, (_, p1) => `"${p1}":`).replace(/\'/g, "\"")) |
| |
| strParse('{name: "jack"}') |
# 其他
# 比较两个对象
当你需要比较两个对象,js 的等于只能判断对象的地址是否相同,当地址不相同的时候无法判断两个对象的键值对是否一致。
| const isEqual = (...objects) => objects.every(obj => JSON.stringify(obj) === JSON.stringify(objects[0])) |
| isEqual({name: 'jack'}, {name: 'jack'}) |
| isEqual({name: 'jack'}, {name: 'jack1'}, {name: 'jack'}) |
# 随机颜色生成
当你需要获取一个随机颜色
| const getRandomColor = () => `#${Math.floor(Math.random() * 0xffffff).toString(16)}` |
| getRandomColor() |
# 颜色格式转换
当你需要将 16 进制的颜色转换成 rgb
| const hexToRgb = hex => hex.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i, (_, r, g, b) => `#${r}${r}${g}${g}${b}${b}`).substring(1).match(/.{2}/g).map((x) => parseInt(x, 16)); |
| hexToRgb('#00ffff'); |
| hexToRgb('#0ff'); |
# 获取随机 ip
当你需要生成一个 ip 地址
| const randomIp = () => |
| Array(4) |
| .fill(0) |
| .map((_, i) => Math.floor(Math.random() * 255) + (i === 0 ? 1 : 0)) |
| .join('.'); |
# uuid
当你需要生成一个 id
| const uuid = (a) => (a ? (a ^ ((Math.random() * 16) >> (a / 4))).toString(16) : ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, uuid)) |
| uuid() |
# 获取 cookie
当你需要将 cookie 转换成对象
| const getCookie = () => document.cookie |
| .split(';') |
| .map((item) => item.split('=')) |
| .reduce((acc, [k, v]) => (acc[k.trim().replace('"', '')] = v) && acc, {}) |
| |
| getCookie() |
# 强制等待
当你需要等待一段时间,但又不想写在 setTimeout 函数中,造成回调地狱
| const sleep = async (t) => new Promise((resolve) => setTimeout(resolve, t)); |
| sleep(2000).then(() => {console.log('time')}); |