欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > 前端常用 utils 工具封装

前端常用 utils 工具封装

2024/10/25 19:23:40 来源:https://blog.csdn.net/zq18877149886/article/details/140772889  浏览:    关键词:前端常用 utils 工具封装

// 函数防抖
export function debounce(fn, interval) {let timerreturn function (this, ...args) {clearTimeout(timer)const context = thislet params = [...args]timer = setTimeout(() => {fn.call(context, ...params)}, interval || 1000)}
}// 函数节流
export function throttle(fn, interval) {let timer = nullreturn function (this, ...args) {const context = thislet params = [...args]if (!timer) {timer = setTimeout(() => {fn.call(context, ...params)timer = null}, interval || 1000)}}
}// 对象数组去重(Map 新的写法,可能不兼容 低版本的浏览器)
export function uniqueArr(arr = [], key) {return Array.from(new Map(arr.map((item) => [item[key], item])))
}// 对象数组去重(老语法)
export function oldUniqueArr(arr = [], key) {if (!key) return new Error("请传入 key")const tempObj = {}const newArray = []for (let i = 0; i < arr.length; i++) {const item = arr[i];if (!tempObj.hasOwnProperty(item[key])) {tempObj[item[key]] = itemnewArray.push(item)}}return newArray
}// 多维数组转成一维数组
export function flattenArray(arr) {return arr.reduce((acc, item) => {return acc.concat(Array.isArray(item) ? flattenArray(item) : item)}, [])
}// 二维数组根据指定的key 转成一维数组
export function flattenAndUniqueByKey(arr, key) {// Array.prototype.flat() 方法将二维数组扁平化为一维数组。const flatArray = arr.flat();// 使用 Map 去重const uniqueMap = new Map();flatArray.forEach(item => {uniqueMap.set(item[key], item);});return Array.from(uniqueMap.values());
}// 二维数组根据指定的key 转成一维数组(旧版语法)
export function oldFlattenAndUniqueByKey(arr, key) {let uniqueObj = {}let uniqueArray = []arr.reduce((acc, childArr) => {childArr.forEach(item => {// 根据传入的 key,做 keyuniqueObj[item[key]] = item;})return acc}, [])for (const key in uniqueObj) {uniqueArray.push(uniqueObj[key])}return uniqueArray
}// const url = 'https://example.com/page?name=Alice&age=30&city=Wonderland';
// 获取 url 参数
export function getQueryParams(url) {const queryParams = {};// 找到 URL 中的查询字符串部分const queryString = url.split('?')[1];// 将查询字符串按 & 分割为参数对const pairs = queryString.split('&');// 遍历每个参数对pairs.forEach(pair => {const [key, value] = pair.split('=');// 对值进行解码并存储到对象中queryParams[decodeURIComponent(key)] = decodeURIComponent(value || '');});return queryParams;
}// 判断两个对象是否相等
export function objectIsEqual(a, b) {if (!a || !b) return falseconst aProps = Object.getOwnPropertyNames(a)const bProps = Object.getOwnPropertyNames(b)if (aProps.length !== bProps.length) return falsefor (let i = 0; i < aProps.length; i++) {const propName = aProps[i]const propA = a[propName]const propB = b[propName]if (!b.hasOwnProperty(propName)) return falseif (propA instanceof Object) {if (!isObjectValueEqual(propA, propB)) return false} else if (propA !== propB) {return false}}return true
}// 延迟执行
export function delay(time) {return new Promise(resolve => setTimeout(resolve, time))
}// 正则验证手机号
export function isPhone(str = "") {const phoneRex =/^[1](([3][0-9])|([4][0,1,4-9])|([5][0-3,5-9])|([6][2,5,6,7])|([7][0-8])|([8][0-9])|([9][0-3,5-9]))[0-9]{8}$/return phoneRex.test(str)
}// 获取数据类型
export function typeOfData(data) {return Object.prototype.toString.call(data).slice(8, -1)
}export function buildTree(items, parentKey = 'parentId', idKey = 'id', rootParentId = null) {// 创建一个 map 来存储每个 id 和其对应的节点const itemMap = new Map();// 遍历数组,初始化每个节点items.forEach(item => {itemMap.set(item[idKey], { ...item, children: [] });});// 组装树形结构const tree = [];itemMap.forEach(node => {const parentId = node[parentKey];if (parentId === rootParentId) {// 如果节点的 parentKey 与 rootParentId 匹配,则为树的根节点tree.push(node);} else {// 否则,找到其父节点,并将其添加到父节点的 children 数组中const parentNode = itemMap.get(parentId);if (parentNode) {parentNode.children.push(node);}}});return tree;
}// 时间格式化
// date 对象 dateFormat("YYYY-mm-dd HH:MM:ss", new Date(毫秒级时间戳)),
export function dateFormat(fmtStr, date) {let fmt = fmtStrlet retconst opt = {"Y+": date.getFullYear().toString(), // 年"m+": (date.getMonth() + 1).toString(), // 月"d+": date.getDate().toString(), // 日"H+": date.getHours().toString(), // 时"M+": date.getMinutes().toString(), // 分"S+": date.getSeconds().toString(), // 秒"s+": date.getMilliseconds().toString(),// 有其他格式化字符需求可以继续添加,必须转化成字符串}for (const k in opt) {if (Object.prototype.hasOwnProperty.call(opt, k)) {ret = new RegExp(`(${k})`).exec(fmt)if (ret) {fmt = fmt.replace(ret[1], ret[1].length === 1 ? opt[k] : opt[k].padStart(ret[1].length, "0"))}}}return fmt
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com