输入数字的规则和对象数组去重

 /**
   * 数字输入框规则
   * @param {*} value 被处理的值
   * @param {*} hasMinusSign 是否可以输入负数
   * @returns 处理后的结果
   */
  checkInputNumber(value, hasMinusSign = false) {
    let val = value
    if (value.length !== 1 && value.substr(-1) === '-') { // 负号在第一个位置
      val = value.substr(0, value.length - 1)
    }
    if (value.indexOf('-') + 1 === value.indexOf('.')) { // 负号后不能接小数点
      val = value.substr(0, value.length - 1)
    }
    if (value.substr(0, value.length - 1).indexOf('.') !== -1 && value.substr(-1) === '.') { // 有小数点时不能再加小数点
      val = value.substr(0, value.length - 1)
    }
    if (hasMinusSign) {
      return val.replace(/[^\d.{0,1}\-{0,1}]/g, '')
    } else {
      return val.replace(/[^\d.{0,1}]/g, '')
    }
  },
 
 
-----------------------
 
 // 对象数组去重
  unique(arr, code) {
    const codeKey = code || 'value'
    const obj = {}
    return arr.reduce((cur, next) => {
      if (!obj[next[codeKey]]) {
        obj[next[codeKey]] = true
        cur.push(next)
      }
      return cur
    }, [])
  },
posted @ 2022-02-11 10:23  MonsterSATSUKI  阅读(38)  评论(0)    收藏  举报