1.检查数字是奇数还是偶数
const isEven = num => num % 2 === 0
isEven(10)
isEven(9)
const isEven = num => (num & 1) === 0
isEven(12)
isEven(11)
2.在两个数字之间生成一个随机数
- 借助
Math.random
生成随机数. - 结果包括最大值最小值.
const getRandomNum = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min
getRandomNum(2, 6)
3.随机生成6位数字验证码
- 借助
Math.random
生成随机数,借助padStart
填充.
const getCode = Math.floor(Math.random() * 1000000).toString().padStart(6, '0')
getCode
4.计算两个日期之间的天数
- 借助
Math.abs
取两个日期之间的绝对值,然后除以1000 * 3600 * 24
(一天的毫秒值). - 参数格式可为yyyy-MM-dd(字符串类型)、yyyy/MM/dd(字符串类型).
const getDays =
(date1, date2) => Math.abs(new Date(date1) - new Date(date2)) / (1000 * 3600 * 24)
getDays('2020-11-29', '2021-11-29')
getDays('2021/01/01', '2021/11/29')
5.查找日期位于一年中的第几天
- 借助
new Date(+date.slice(0, 4), 0, 0))
获取当年最开始的时间,然后除以一天的毫秒数.
const getDayOfYear =
date => Math.floor((new Date(date) - new Date(+date.slice(0, 4), 0, 0)) / (1000 * 3600 * 24))
getDayOfYear('2021-02-02')
6.处理当前时间
- 借助
toTimeString()
方法将Date对象
的时间部分转换为字符串. - 以
hour::minutes::seconds
格式输出当前时间.
const curTime = new Date().toTimeString().slice(0, 8)
curTime
7.获取浏览器Cookie的值
- 借助
document.cookie
来查找cookie的值.
const getCookie =
name => `; ${document.cookie}`.split(`; ${name}=`).pop().split(';').shift()
getCookie('_ga')
8.将数字转化为千分位格式
- 借助
toLocaleString('en-US')
进行转化.
const toMark = num => num.toLocaleString('en-US')
toMark(1213865.123)
9.数组去重
const delRepeat = arr => [...new Set(arr)]
delRepeat([1, 2, 2, 3, 3, 3, 4, 5, 5])
10.数组合并
- 两种合并数组的 方法,
concat()
方法和扩展运算符...
const arr1 = [1, 2]
const arr2 = ['Tom', 'Bob']
const mergeArr = arr1.concat(arr2)
const mergeArr = [].concat(arr1, arr2)
const mergeArr = [...arr1, ...arr2]
mergeArr
11.随机排列数组
- 借助
sort()
方法及Math.random()
打乱数组.
const disorderArr = [1, 5, 10, 25, 40, 100].sort(() => 0.5 - Math.random())
disorderArr
12.多维数组转一维数组
const deepFlatten =
arr => [].concat(...arr.map(n => (Array.isArray(n) ? deepFlatten(n) : n)))
deepFlatten([1, [2, 3], [4, [5, 6]]])
13.检查数组是否为空
- 先判断是否为数组,再判断数组的长度.
- 非空数组返回
true
, 否则为false
.
const isEmptyArr = arr => Array.isArray(arr) && arr.length > 0
isEmptyArr([3, 2, 1])
isEmptyArr([])
14.字符串反转
- 先
split()
分割,然后reverse()
反转,再join()
拼接.
const reverse = str => str.split('').reverse().join('')
reverse('hello world')
15.获取JS语言的实际类型
typeof
只能判断基本类型,通过Object.prototype.toString
可以判断某个对象属于哪种内置类型.
const judgeType = obj => Object.prototype.toString.call(obj).slice(8, -1)
judgeType('')
judgeType(0)
judgeType()
judgeType(null)
judgeType({})
judgeType([])
judgeType(() => {})
judgeType(Symbol())
16.身份证的正则表达式
const IDReg = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx]$)/;
IDRrg.test(511324200001011111)
17.将RGB转换为十六进制
const colorHex =
(r, g, b) => "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)
colorHex(255, 255, 255)
18.滚动到页面顶部
- 借助
window.scrollTo(0, 0)
方法滚动到顶部.
const goTop = () => window.scrollTo(0, 0)
goTop()
19.将文字复制到剪贴板
- 借助
navigator.clipboard.writeText
将文本复制到剪切板.
const copyText = async (text) => await navigator.clipboard.writeText(text)
copyText('hello World')
20.检查设备类型
- 借助
navigator.userAgent
判断是移动设备还是电脑设备.
const judgeDeviceType =
() => /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|OperaMini/i.test(navigator.userAgent) ? 'Mobile' : 'Desktop';
judgeDeviceType()