ES6便捷语法

/**
 * 解构赋值
 * */
const obj = {
  a: 1,
  b: 2,
  c: 3,
  d: 4,
  e: 5
}
const { a, b, c, d, e } = obj
const f = a + d
const g = c + e
// 如果想创建的变量名和对象的属性名不一致,可以这么写
const { a: a1 } = obj
console.log(a1) // 1
// 解构的对象不能为undefined、null。否则会报错,故要给被解构的对象一个默认值。
const { a, b, c, d, e } = obj || {}
/**
 * 合并数据
 * */
const a = [1, 2, 3]
const b = [1, 5, 6]
const c = [...new Set([...a, ...b])] //[1,2,3,5,6]

const obj1 = {
  a: 1
}
const obj2 = {
  b: 1
}
const obj = { ...obj1, ...obj2 } //{a:1,b:1}
/**
 * 拼接字符串
 * */
const name = '小明'
const score = 59
const result = `${name}${score > 60 ? '的考试成绩及格' : '的考试成绩不及格'}`
/**
 * if中判断条件
 * */
const condition = [1, 2, 3, 4]

if (condition.includes(type)) {
  //...
}
/**
 * 列表搜索
 * */
const a = [1, 2, 3, 4, 5]
const result = a.find(item => {
  return item === 3
})
/**
 * 扁平化数组
 * */
const deps = {
  采购部: [1, 2, 3],
  人事部: [5, 8, 12],
  行政部: [5, 14, 79],
  运输部: [3, 64, 105]
}
let member = Object.values(deps).flat(Infinity)
/**
 * 获取对象属性值
 * */
// const name = obj && obj.name;
const name = obj?.name
/**
 * 添加对象属性
 * */
let obj = {}
let index = 1
obj[`topic${index}`] = '话题内容'
/**
 * 输入框非空
 * */
// if(value !== null && value !== undefined && value !== ''){
//   //...
// }
if (value ?? '' !== '') {
  //...
}

 

// 生成指定范围随机数
const randomNum = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min
console.log(randomNum(1, 10))

// 数组乱序
const arrScrambling = arr => {
  for (let i = 0; i < arr.length; i++) {
    const randomIndex = Math.round(Math.random() * (arr.length - 1 - i)) + i
    ;[arr[i], arr[randomIndex]] = [arr[randomIndex], arr[i]]
  }
  return arr
}
console.log(arrScrambling([1, 2, 3, 4, 5, 6, 7, 8, 9]))

// 在数组中获取随机数
const sample = arr => arr[Math.floor(Math.random() * arr.length)]
console.log(sample([4, 9, 6, 5, 8, 1, 3, 7, 2]))

// 生成随机字符串
const randomString = len => {
  let chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz123456789'
  let strLen = chars.length
  let randomStr = ''
  for (let i = 0; i < len; i++) {
    randomStr += chars.charAt(Math.floor(Math.random() * strLen))
  }
  return randomStr
}
console.log(randomString(7))

// 字符串首字母大写
const fistLetterUpper = str => {
  return str.charAt(0).toUpperCase() + str.slice(1)
}

console.log(fistLetterUpper('cheng'))

// 手机号中间四位变成*
const telFormat = tel => {
  tel = String(tel)
  return tel.substr(0, 3) + '****' + tel.substr(7)
}

console.log(telFormat(13500000000))

// 驼峰命名转换成短横线命名
const getKebabCase = str => {
  return str.replace(/[A-Z]/g, item => '-' + item.toLowerCase())
}
console.log(getKebabCase('salseTrend'))

// 短横线命名转换成驼峰命名
const getCamelCase = str => {
  return str.replace(/-([a-z])/g, (i, item) => item.toUpperCase())
}
console.log(getCamelCase('salse-trend'))

// 数字转化为大写金额
const digitUppercase = n => {
  const fraction = ['角', '分']
  const digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
  const unit = [
    ['元', '万', '亿'],
    ['', '拾', '佰', '仟']
  ]
  n = Math.abs(n)
  let s = ''
  for (let i = 0; i < fraction.length; i++) {
    s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '')
  }
  s = s || '整'
  n = Math.floor(n)
  for (let i = 0; i < unit[0].length && n > 0; i++) {
    let p = ''
    for (let j = 0; j < unit[1].length && n > 0; j++) {
      p = digit[n % 10] + unit[1][j] + p
      n = Math.floor(n / 10)
    }
    s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s
  }
  return s
    .replace(/(零.)*零元/, '元')
    .replace(/(零.)+/g, '零')
    .replace(/^整$/, '零元整')
}
console.log(digitUppercase(1000000))

// 数字转化为中文数字
const intToChinese = value => {
  const str = String(value)
  const len = str.length - 1
  const idxs = ['', '十', '百', '千', '万', '十', '百', '千', '亿', '十', '百', '千', '万', '十', '百', '千', '亿']
  const num = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  return str.replace(/([1-9]|0+)/g, ($, $1, idx, full) => {
    let pos = 0
    if ($1[0] !== '0') {
      pos = len - idx
      if (idx === 0 && $1[0] === 1 && idxs[len - idx] === '十') {
        return idxs[len - idx]
      }
      return num[$1[0]] + idxs[len - idx]
    } else {
      let left = len - idx
      let right = len - idx + $1.length
      if (Math.floor(right / 4) - Math.floor(left / 4) > 0) {
        pos = left - (left % 4)
      }
      if (pos) {
        return idxs[pos] + num[$1[0]]
      } else if (idx + $1.length >= len) {
        return ''
      } else {
        return num[$1[0]]
      }
    }
  })
}
console.log(intToChinese(1234567890))

 

posted @ 2021-10-12 10:47  ronle  阅读(79)  评论(0编辑  收藏  举报