js 一行代码

数组洗牌
const shuffleArray = item => item.sort(() => Math.random() - 0.5)
const arr1 = [21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
console.log(shuffleArray(arr1))
复制到剪贴板
const copyToClipboard = text =>
  navigator.clipboard &&
  navigator.clipboard.writeText &&
  navigator.clipboard.writeText(text)
copyToClipboard('Hello World!')
数组去重
const getUnique = arr => [...new Set(arr)]
const arr = [1, 1, 2, 3, 3, 4, 4, 5, 5]
console.log(getUnique(arr))
滚动到顶部
const scrollToTop = element =>
  element.scrollIntoView({ behavior: 'smooth', block: 'start' })
console.log(scrollToTop())
滚动到底部
const scrollToBottom = element =>
  element.scrollIntoView({ behavior: 'smooth', block: 'end' })
console.log(scrollToBottom())
生成随机颜色
const generateRandomHexColor = () =>
  `#${Math.floor(Math.random() * 0xffffff).toString(16)}`
console.log(generateRandomHexColor())

你可能感兴趣的:(javascript,前端)