生成随机ID

  1. // 生成长度为11的随机字母数字字符串
  2. Math.random().toString(36).substring(2);

获取URL的查询参数

  1. // 获取URL的查询参数
  2. q={};location.search.replace(/([^?&=]+)=([^&]+)/g,(_,k,v)=>q[k]=v);q;

数组混淆

  1. // 随机更改数组元素顺序,混淆数组
  2. (arr) => arr.slice().sort(() => Math.random() - 0.5)

生成随机十六进制代码(生成随机颜色)

  1. '#' + Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6, '0');

去除空格

  1. const trim = (str) => {
  2. return str.replace(/^\s*|\s*$/g, '')
  3. }