js常用组件

//转化时间
function formatDate(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, “0”);
const day = String(date.getDate()).padStart(2, “0”);
const hours = String(date.getHours()).padStart(2, “0”);
const minutes = String(date.getMinutes()).padStart(2, “0”);
const seconds = String(date.getSeconds()).padStart(2, “0”);
return ${year}-${month}-${day} ${hours}:${minutes}:${seconds};
}

模糊搜索
export const fuzzyQuery = (list, keyWord, attribute = ‘name’) => {
const reg = new RegExp(keyWord)
const arr = []
for (let i = 0; i < list.length; i++) {
if (reg.test(list[i][attribute])) {
arr.push(list[i])
}
}
return arr
}
const list = [
{ id: 1, name: ‘树哥’ },
{ id: 2, name: ‘黄老爷’ },
{ id: 3, name: ‘张麻子’ },
{ id: 4, name: ‘汤师爷’ },
{ id: 5, name: ‘胡万’ },
{ id: 6, name: ‘花姐’ },
{ id: 7, name: ‘小梅’ }
]
fuzzyQuery(list, ‘树’, ‘name’) // [{id: 1, name: ‘树哥’}]

遍历树节点
export const foreachTree = (data, callback, childrenName = ‘children’) => {
for (let i = 0; i < data.length; i++) {
callback(data[i])
if (data[i][childrenName] && data[i][childrenName].length > 0) {
foreachTree(data[i][childrenName], callback, childrenName)
}
}
}

校验数据类型
export const typeOf = function(obj) {
return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase()
}

防抖
export const debounce = (() => {
let timer = null
return (callback, wait = 800) => {
timer&&clearTimeout(timer)
timer = setTimeout(callback, wait)
}
})()

节流
export const throttle = (() => {
let last = 0
return (callback, wait = 800) => {
let now = +new Date()
if (now - last > wait) {
callback()
last = now
}
}
})()

手机号脱敏
export const hideMobile = (mobile) => {
return mobile.replace(/^(\d{3})\d{4}(\d{4})$/, “$1****$2”)
}

解析URL参数
export const getSearchParams = () => {
const searchPar = new URLSearchParams(window.location.search)
const paramsObj = {}
for (const [key, value] of searchPar.entries()) {
paramsObj[key] = value
}
return paramsObj
}

判断手机是Andoird还是IOS
export const getOSType=() => {
let u = navigator.userAgent, app = navigator.appVersion;
let isAndroid = u.indexOf(‘Android’) > -1 || u.indexOf(‘Linux’) > -1;
let isIOS = !!u.match(/(i[^;]+;( U;)? CPU.+Mac OS X/);
if (isIOS) {
return 1;
}
if (isAndroid) {
return 2;
}
return 3;
}

数组对象根据字段去重
arr 要去重的数组 key 根据去重的字段名
export const uniqueArrayObject = (arr = [], key = ‘id’) => {
if (arr.length === 0) return
let list = []
const map = {}
arr.forEach((item) => {
if (!map[item[key]]) {
map[item[key]] = item
}
})
list = Object.values(map)

return list
}
const responseList = [
{ id: 1, name: ‘树哥’ },
{ id: 2, name: ‘黄老爷’ },
{ id: 3, name: ‘张麻子’ },
{ id: 1, name: ‘黄老爷’ },
{ id: 2, name: ‘张麻子’ },
{ id: 3, name: ‘树哥’ },
{ id: 1, name: ‘树哥’ },
{ id: 2, name: ‘黄老爷’ },
{ id: 3, name: ‘张麻子’ },
]

uniqueArrayObject(responseList, ‘id’)

滚到页面顶部
export const scrollToTop = () => {
const height = document.documentElement.scrollTop || document.body.scrollTop;
if (height > 0) {
window.requestAnimationFrame(scrollToTop);
window.scrollTo(0, height - height / 8);
}
}

滚动到元素位置
export const smoothScroll = element =>{
document.querySelector(element).scrollIntoView({
behavior: ‘smooth’
});
};
smoothScroll(‘#target’);

uuid
export const uuid = () => {
const temp_url = URL.createObjectURL(new Blob())
const uuid = temp_url.toString()
URL.revokeObjectURL(temp_url) //释放这个url
return uuid.substring(uuid.lastIndexOf(‘/’) + 1)
}
uuid()

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值