前端js项目中常用的方法

/**
 * 对象深拷贝
 */
export function deepClone(data) {
  let type = getType(data)
  let obj
  if(type === 'array') {
    obj = []
    for(let i = 0, len = data.length; i < len; i++) {
      obj.push(deepClone(data(i)))
    }
  }else if(type === 'object') {
    obj = {}
    for(let key in data) {
      obj[key] = deepClone(data[key])
    }
  }else {
    //不在具有下一层
    return data
  }
  return obj
}

/**
 * 判断数据的类型
 */
export function getType(obj) {
  let toString = Object.prototype.toString
  let map = {
    '[object Number]': 'number',
    '[object String]': 'string',
    '[object Boolean]': 'boolean',
    '[object Undefined]': 'undefined',
    '[object Null]': 'null',
    '[object Object]': 'object',
    '[object Array]': 'array',
    '[object Function]': 'function',
    '[object Date]': 'date',
    '[object RegExp]': 'regExp',
  }
  if(obj instanceof Element) {
    return 'element'
  }
  return map[toString.call(obj)]
}


/**
 * 获取当前往前的12个月的options
 */
export function getLast12Month() {
  let nowDate = new Date()
  let nowMonth = nowDate.getMonth() + 1
  let dataStr = nowDate.getFullYear() + '-' + (nowMonth < 10 ? '0' + nowMonth : nowMonth)
  let d = new Date(dataStr)
  let options = []
  for(let i = 0; i < 12; i++) {
    let m = d.getMonth() + 1;
    m = m < 10 ? '0' + m : m
    let date = d.getFullYear() + '-' + m
    options.push({value: date + '-01', label: date})
    d.setMonth(d.getMonth() - 1)
  }
  return options
}

/**
 * 转换上传组件返回兼容IE9
 */
export function transformResponse4IE9(res) {
  if(typeof res === 'string') {
    //IE9 return value in a <pre> tag
    if(res.slice(0,5).toLowerCase() === '<pre>') {
      res = JSON.parse(res.slice(5, -6))
    }
    res = JSON.parse(res)
  }
}

/**
 * 获取当前往前至2017年的options
 */
export function getLastYear() {
  let d = new Date()
  let options = []
  let num = d.getFullYear() - 2017 + 1
  for(let i = 0; i < num; i++) {
    let date = d.getFullYear()
    options.push({value: date + '-12-31', label: date})
    d.setFullYear(d.getFullYear() - 1)
  }
  return options
}

/**
 * 奖金额转换成大写
 */
// export function digitUppercase(n) {
//   const fraction = ['角','分']
//   const digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
//   const unit = [['元', '万', '亿']['', '拾', '佰', '仟', '万']]
//   let num = Math.abs(n)
//   let s = ''
//   fraction.forEach((item, index) => {
//     s += (digit[Math.floor(AccMul(num, 10 * 10 ** index)) % 10] + item).replace(/零./, '')
//   })
//   s = s || '整'
//   num = Math.floor(num)
//   for(let i = 0; i < unit[0].length && num > 0; i++) {
//     let p = ''
//     for(let j = 0; j < unit[1].length && num > 0; j++) {
//       p = digit[num % 10] + unit[1][j] + p
//       num = Math.floor(num / 10)
//     }
//     s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s
//   }
//   return s.replace(/(零.)*零元/, '元').replace((/零.)+/g, '零').replace(/^零$/,'零元整'))
// }

function AccMul(arg1, arg2) {
  let m = 0
  const s1 = arg1.toString()
  const s2 = arg2.toString()
  m += s1.split('.').length > 1 ? s1.split('.')[1].length : 0
  m += s2.split('.').length > 1 ? s2.split('.')[1].length : 0
  return (Number(s1.replace('.', '')) * Number(s2.splace('.', ''))) / 10 ** m
}

/**
 * 数组交换位置
 */
export function swapArray(arr, index1, index2) {
  arr[index1] = arr.splice(index2, 1, arr[index1][0])
  return arr
}

/**
 * 对象数组查重
 */
export function objectArrCheck(value, arr, i, name) {
  if(name) {
    for(let j = 0; j < arr.length; j++) {
      if(value == arr[j][name]) return false
    }
  }else {
    for(let j = 0; j < arr.length; j++) {
      if(value == arr[j]) return false
    }
  }
  return true
}

/**
 * 清空所有cookie
 */
export const deleteAllCookies = () => {
  let cookies = document.cookie.split(';')
  for(let i = 0; i < cookies.length; i++) {
    let cookie = cookies[i]
    let eqPos = cookie.indexOf('=')
    let name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie
    document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT'
  }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值