25 个杀手级 JavaScript 单行代码让你看起来像个专业人士

1.将内容复制到剪贴板

为了提高网站的用户体验,我们经常需要将内容复制到剪贴板,以便用户将其粘贴到指定位置。

const copyToClipboard = (content) => navigator.clipboard.writeText(content)
copyToClipboard("Hello fatfish")

2.获取鼠标选择

你以前遇到过这种情况吗?

我们需要获取用户选择的内容。

const getSelectedText = () => window.getSelection().toString()
getSelectedText()

3.打乱数组

打乱数组?这在彩票程序中很常见,但并不是真正随机的。

const shuffleArray = array => array.sort(() => Math.random() - 0.5)
shuffleArray([ 1, 2,3,4, -1, 0 ]) // [3, 1, 0, 2, 4, -1]

4.将 rgba 转换为十六进制

我们可以将 rgba 和十六进制颜色值相互转换。

const rgbaToHex = (r, g, b) => "#" + [r, g, b].map(num => parseInt(num).toString(16).padStart(2, '0')).join('')
rgbaToHex(0, 0 ,0) // #000000rgbaToHex(255, 0, 127) //#ff007f

5.将十六进制转换为 rgba

const hexToRgba = hex => {  const [r, g, b] = hex.match(/\w\w/g).map(val => parseInt(val, 16))  return `rgba(${r}, ${g}, ${b}, 1)`;}
hexToRgba('#000000') // rgba(0, 0, 0, 1)hexToRgba('#ff007f') // rgba(255, 0, 127, 1)

6.获取多个数字的平均值

使用 reduce 我们可以非常方便地获取一组数组的平均值。

const average = (...args) => args.reduce((a, b) => a + b, 0) / args.length
average(0, 1, 2, -1, 9, 10) // 3.5

7.检查数字是偶数还是奇数

你如何判断数字是奇数还是偶数?

const isEven = num => num % 2 === 0
isEven(2) // trueisEven(1) // false

8.删除数组中的重复元素

要删除数组中的重复元素,使用 Set 会变得非常容易。

const uniqueArray = (arr) => [...new Set(arr)]
uniqueArray([ 1, 1, 2, 3, 4, 5, -1, 0 ]) // [1, 2, 3, 4, 5, -1, 0]

9.检查对象是否为空对象

判断对象是否为空容易吗?

const isEmpty = obj => Reflect.ownKeys(obj).length === 0 && obj.constructor === Object
isEmpty({}) // trueisEmpty({ name: 'fatfish' }) // false

10.反转字符串

const reverseStr = str => str.split('').reverse().join('')
reverseStr('fatfish') // hsiftaf

11.计算两个日期之间的间隔

const dayDiff = (d1, d2) => Math.ceil(Math.abs(d1.getTime() - d2.getTime()) / 86400000)
dayDiff(new Date("2023-06-23"), new Date("1997-05-31")) // 9519

12.找出日期所在的年份

const dayInYear = (d) => Math.floor((d - new Date(d.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24)
dayInYear(new Date('2023/06/23'))// 174

13.将字符串的首字母大写

const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1)
capitalize("hello fatfish")  // Hello fatfish

14.生成指定长度的随机字符串

const generateRandomString = length => [...Array(length)].map(() => Math.random().toString(36)[2]).join('')
generateRandomString(12) // cysw0gfljoyxgenerateRandomString(12) // uoqaugnm8r4s

15.获取两个整数之间的随机整数

const random = (min, max) => Math.floor(Math.random() * (max - min + 1) + min)
random(1, 100) // 27random(1, 100) // 84random(1, 100) // 55

16.指定数字四舍五入

const round = (n, d) => Number(Math.round(n + "e" + d) + "e-" + d)
round(3.1415926, 3) //3.142round(3.1415926, 1) //3.1

17.清除所有 cookie

const clearCookies = document.cookie.split(';').forEach(cookie => document.cookie = cookie.replace(/^ +/, '').replace(/=.*/, `=;expires=${new Date(0).toUTCString()};path=/`))

18.检测是否为暗模式

const isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
console.log(isDarkMode)

19.滚动到页面顶部

const goToTop = () => window.scrollTo(0, 0)
goToTop()

20.确定是否为 Apple 设备

const isAppleDevice = () => /Mac|iPod|iPhone|iPad/.test(navigator.platform)
isAppleDevice()

21.随机布尔值

const randomBoolean = () => Math.random() >= 0.5
randomBoolean()

22.获取变量的类型

const typeOf = (obj) => Object.prototype.toString.call(obj).slice(8, -1).toLowerCase()
typeOf('')     // stringtypeOf(0)      // numbertypeOf()       // undefinedtypeOf(null)   // nulltypeOf({})     // objecttypeOf([])     // arraytypeOf(0)      // numbertypeOf(() => {})  // function

23.确定当前选项卡是否处于活动状态

const checkTabInView = () => !document.hidden

24.检查元素是否处于焦点

const isFocus = (ele) => ele === document.activeElement

25.随机 IP

const generateRandomIP = () => {  return Array.from({length: 4}, () => Math.floor(Math.random() * 256)).join('.');}
generateRandomIP() // 220.187.184.113generateRandomIP() // 254.24.179.151

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值