常用(实用)函数方法封装汇总(未完待续)

以下函数的使用方法举例:
例如:
在某public.js文件里编写这些方法,在引用文件里先导入:

import { isObjectValueEqual } from ‘@/utils/public’

然后使用:

isObjectValueEqual(oldValue, newValue)


1.js查找对象在对象数组中的位置

for (let i = 0, iLen = arr.length; i < iLen; i++) {
     if (arr[i].batchid === obj.batchid) {
       return i
     }
}

2.JS查找对象是否在对象数组中(兼容ie8)

console.log(JSON.stringify(arr).indexOf(JSON.stringify(json1))!=-1);

3.睡眠排序法

const arr = [11, 3, 998, 5455, 1, 152, 990]
        arr.forEach((value, index) => {
          setTimeout(() => {
            this.res.push(value)
          }, value);
 })

4.比较两个对象是否相等

export const isObjectValueEqual = (oldValue, newValue) => {
  var oldValueProps = Object.getOwnPropertyNames(oldValue)
  var newValueProps = Object.getOwnPropertyNames(newValue)
  if (oldValueProps.length !== newValueProps.length) {
    return false
  }
  for (var i = 0; i < oldValueProps.length; i++) {
    var propName = oldValueProps[i]
    if (oldValue[propName] !== newValue[propName]) {
      return false
    }
  }
  return true
}

5.获取两数组不同元素

export const getArrDifference = (arr1, arr2) => {
  return arr1.concat(arr2).filter(function(v, i, arr) {
    return arr.indexOf(v) === arr.lastIndexOf(v)
  })
}

6.获取两数组相同元素

export const getArrEqual = (arr1, arr2) => {
  const newArr = []
  for (let i = 0; i < arr2.length; i++) {
    for (let j = 0; j < arr1.length; j++) {
      if (arr1[j] === arr2[i]) {
        newArr.push(arr1[j])
      }
    }
  }
  return newArr
}

7.判断时间是否在查询时间内

export const isTimes = (newDate, startDate, endDate) => {
  const preDate = new Date(newDate)
  const a = preDate.getTime() - startDate.getTime()
  const b = preDate.getTime() - endDate.getTime()
  const time1 = parseInt(a / (1000 * 60 * 60 * 24))
  const time2 = parseInt(b / (1000 * 60 * 60 * 24))
  if (time1 < 0 || time2 > 0) {
    return false
  } else {
    return true
  }
}

8.判断输入数据类型

export const isType = {
  isObject: (val) => {
    return Object.prototype.toString.call(val) == '[object Object]'
  },
  isArray: (val) => {
    return Object.prototype.toString.call(val) == '[object Array]'
  },
  isNULL: (val) => {
    return Object.prototype.toString.call(val) == '[object Null]'
  },
  isString: (val) => {
    return Object.prototype.toString.call(val) == '[object String]'
  },
  isInteger: (val) => {
    return Number.isInteger(val)
  },
}

9.格式化时间

export function parseTime(time, cFormat) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (typeof time === 'string') {
      if (/^[0-9]+$/.test(time)) {
        // support "1548221490638"
        time = parseInt(time)
      } else {
        // support safari
        // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
        time = time.replace(new RegExp(/-/gm), '/')
      }
    }

    if (typeof time === 'number' && time.toString().length === 10) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay(),
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    return value.toString().padStart(2, '0')
  })
  return time_str
}

引入:

import { parseTime } from ‘@/utils’

使用:
let aaa = parseTime(new Date())
let aaa = parseTime(new Date(), ‘{y}-{m}-{d} {h}-{i}-{s}’)

10.字符串中是否有小写字符

hasLowercase: function (str) {
            var result = str.match(/^.*[a-z]+.*$/)
            if (result == null) return false
            return true
          },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值