本篇博客是关于常用js功能函数汇总,对你有帮助的话记得点赞➕关注!
1、判断时间戳格式是否是毫秒
const isMillisecond = timeStamp => {
const timeStr = String(timeStamp);
return timeStr.length > 10;
};
2、传入的时间戳是否早于当前时间戳
const isEarly = (timeStamp, currentTime) => {
return timeStamp < currentTime;
};
3、如果传入的数值小于10,即位数只有1位,则在前面补充0
const getHandledValue = num => {
return num < 10 ? "0" + num : num;
};
4、判断要查询的数组是否至少有一个元素包含在目标数组中
export const hasOneOf = (targetarr, arr) => {
return targetarr.some(_ => arr.indexOf(_) > -1);
};
5、判断两个对象是否相等,这两个对象的值只能是数字或字符串
export const objEqual = (obj1, obj2) => {
const keysArr1 = Object.keys(obj1);
const keysArr2 = Object.keys(obj2);
if (keysArr1.length !== keysArr2.length) return false;
else if (keysArr1.length === 0 && keysArr2.length === 0) return true;
else
return !keysArr1.some(key => obj1[key] != obj2[key]);
};
6、 返回 n-m之间随机数
export const getRandom = (n, m) => {
n = Number(n);
m = Number(m);
if (n > m) {
var temp = n;
n = m;
m = temp;
}
return Math.floor(Math.random() * (m - n) + n);
};
7、 随机生成校验码
export const randomCode = num => {
let codeStr = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
let str = "";
for (var i = 0; i < num; i++) {
var ran = getRandom(0, 60);
str += codeStr.charAt(ran);
}
return str;
};
8、 深度合并数据
export function deepMerge(obj1, obj2) {
for (let key in obj2) {
obj1[key] =
obj1[key] &&
obj1[key].toString() === "[object Object]" &&
obj2[key] && obj2[key].toString() === "[object Object]" ?
deepMerge(obj1[key], obj2[key]) :
(obj1[key] = obj2[key]);
}
return obj1;
}
9、 格式化日期
export function formatDate({
date = new Date(),
format = "yyyy-MM-dd",
type,
next
}) {
const opt = {
"y+": date.getFullYear(),
"M+": date.getMonth() + 1,
"d+": date.getDate(),
"h+": date.getHours(),
"m+": date.getMinutes(),
"s+": date.getSeconds()
};
if (type && next) {
switch (type) {
case "y":
opt["y+"] = opt["y+"] - next;
break;
case "m":
opt["y+"] = opt["y+"] - parseInt(next / 12);
if (next % 12 < opt["M+"]) {
opt["M+"] = opt["M+"] - (next % 12);
} else {
opt["y+"] = opt["y+"] - parseInt(next / 12) - 1;
opt["M+"] = 12 + opt["M+"] - (next % 12);
}
break;
case "d":
let timer = 1000 * 60 * 60 * 24 * next;
let newDate = new Date(date.getTime() - timer);
opt["y+"] = newDate.getFullYear();
opt["M+"] = newDate.getMonth() + 1;
opt["d+"] = newDate.getDate();
break;
default:
break;
}
}
for (var k in opt) {
if (k == "y+") {
if (/(y+)/.test(format))
format = format.replace(
RegExp.$1,
(opt[k] + "").substr(4 - RegExp.$1.length)
);
} else {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(
RegExp.$1,
RegExp.$1.length == 1 ?
opt[k] :
("00" + opt[k]).substr(("" + opt[k]).length)
);
}
}
}
return format;
}
10、 函数防抖
export function debounce(fn, delay) {
let timeout = null;
return function (e) {
clearTimeout(timeout);
timeout = setTimeout(() => {
fn.apply(this, arguments);
}, delay);
};
}
11、 函数截流
export function throttle(handler, wait) {
var lastTime = 0;
return function () {
var nowTime = new Date().getTime();
if (nowTime - lastTime > wait) {
handler();
lastTime = nowTime;
}
};
}
12、 判断是否为对象
function isObject(obj) {
return Object.prototype.toString.call(obj) === "[object Object]";
}
13、 判断是否数组
function isArray(arr) {
return Object.prototype.toString.call(arr) === "[object Array]";
}
14、 数组扁平化
export function flatten(arr, childKey, selKey, onlyChild = false) {
if (!Array.isArray(arr)) return arr;
return arr.reduce((pre, cur) => {
if (childKey && Array.isArray(cur[childKey]) && cur[childKey].length > 0) {
if (!onlyChild && selKey) pre.push(cur[selKey]);
return pre.concat(flatten(cur[childKey], childKey, selKey, onlyChild))
} else {
return pre.concat(cur[selKey])
}
}, [])
}