防抖和节流
const debounce = (fn, wait, immediate) => {
let timer = null;
return function (...args) {
if (timer) clearTimeout(timer);
if (immediate && !timer) {
fn.call(this, args);
}
timer = setTimeout(() => {
fn.call(this, args);
}, wait);
};
};
const betterFn = debounce(() => console.log("fn 防抖执行了"), 1000, true);
document.addEventListener("scroll", betterFn);
function throttle(fn,wait){
let pre = 0;
return function(...args){
let now = Date.now();
if( now - pre >= wait){
fn.apply(this,args);
pre = now;
}
}
}
function handle(){
console.log(Math.random());
}
window.addEventListener("mousemove",throttle(handle,1000));
对象深浅拷贝
1. Object.assign(target,source)
2. es6对象扩展运算符。
function deepClone(obj) {
if (!obj || typeof obj !== "object") return;
let newObj = Array.isArray(obj) ? [] : {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
newObj[key] = typeof obj[key] === "object" ? deepClone(obj[key]) : obj[key];
}
}
return newObj;
}
数组扁平化
function flatten(arr) {
return arr.reduce((result, item) => {
return result.concat(Array.isArray(item) ? flatten(item) : item);
}, []);
}
数组对象转树
const datas = [
{ id: 2, pid: 0, path: '/course', title: '课程管理' },
{ id: 3, pid: 2, path: '/operate', title: '课程操作' },
{ id: 5, pid: 2, path: '/add', title: '增加课程' },
{ id: 6, pid: 0, path: '/student', title: '学生管理' },
{ id: 7, pid: 6, path: '/studentAdd', title: '添加学生' },
{ id: 8, pid: 6, path: '/studentOperate', title: '学生操作' }
]
function formarRouterTree (data) {
let parents = data.filter(p => p.pid === 0),
children = data.filter(c => c.pid !== 0);
dataToTree(parents, children);
function dataToTree (parents, children) {
parents.map((p) => {
children.map((c, i) => {
if (c.pid === p.id) {
let _c = JSON.parse(JSON.stringify(children));
_c.splice(i, 1);
dataToTree([c], _c);
if (p.children) {
p.children.push(c);
} else {
p.children = [c];
}
}
})
})
}
return parents;
}
const routers = formarRouterTree(datas)
console.log(routers)
const firstNode = datas.filter(v => !v.pid)
function loop(_datas, ret) {
for (const item of ret) {
const children = _datas.filter(v => v.pid === item.id)
item.children = loop(_datas, children);
}
return ret;
};
const result = loop(datas, firstNode);
console.log(result, 'result');
getTreeData(data, id = "id", pid = "pid") {
let cloneData = JSON.parse(JSON.stringify(data));
return cloneData.filter(parent => {
let branchArr = cloneData.filter(
child => parent[id] == child[pid]
);
for (let i = 0; i < branchArr.length; i++) {
branchArr.parent_nickname = parent.nickname;
}
branchArr.length > 0 ? (parent["children"] = branchArr) : "";
return parent[pid] === null;
});
}
根据条件一维数组转二维数组
const arr = [
{ name: '1', type: 1 },
{ name: '2', type: 1 },
{ name: '3', type: 1 },
{ name: '4', type: 2 },
{ name: '5', type: 2 },
{ name: '6', type: 2 },
{ name: '7', type: 2 },
]
const map1 = arr.reduce((p, c) => [p[c.type] = p[c.type] || [], p[c.type].push(c), p][2], {});
const result = Object.keys(map1).map(i => map1[i])
const result = Object.values(arr.reduce((res, item) => {
res[item.type] ? res[item.type].push(item) : res[item.type] = [item];
return res;
}, {}));
点击复制内容
function copyFn(event, cpId) {
var range = document.createRange();
range.selectNode(document.getElementById(cpId));
var selection = window.getSelection();
if (selection.rangeCount > 0) selection.removeAllRanges();
selection.addRange(range);
document.execCommand('Copy');
alert('复制成功,快去添加吧')
}
滑轮切换fixed固定底部条
.bar{width:100%;height:70px;background-color:orange;position:relative;text-align:center;line-height:70px;}
.fixed-bar{position:fixed;left:0;bottom:0;}
<div id="adBar"><div class="bar fixed-bar">切换fixed</div></div>
$(window).scroll(function(){
var position = $('#adBar').position().top;
var barH = $('.bar').height();
var winH = $(window).height();
var winScrollTop = $(window).scrollTop();
if(winScrollTop >= position - winH + barH){
$('.bar').removeClass('fixed-bar');
}else{
$('.bar').addClass('fixed-bar');
}
})
bottomShow();
function bottomShow() {
var fixedBottom = $('#fixedBottom');
var bTop = fixedBottom.offset().top - $(window).height() + fixedBottom.height();
var rTop = $('.reg').offset().top + $('.reg').height();
$(window).scroll(function () {
var sTop = $(this).scrollTop();
if (sTop > rTop) {
fixedBottom.show();
if (sTop > bTop) {
fixedBottom.removeClass('fixed');
} else {
fixedBottom.addClass('fixed');
}
} else {
fixedBottom.hide();
}
})
}
判断android & ios or others
const isMobile = {
Android: function () {
return navigator.userAgent.match(/Android/i) ? true : false;
},
BlackBerry: function () {
return navigator.userAgent.match(/BlackBerry/i) ? true : false;
},
iOS: function () {
return navigator.userAgent.match(/iPhone|iPad|iPod/i) ? true : false;
},
Windows: function () {
return navigator.userAgent.match(/IEMobile/i) ? true : false;
},
any: function () {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows());
}
};
if (isMobile.Android()) {
window.location.href = 'android.htm'
}
获取URL中的参数值
function getQueryString(params) {
var reg = new RegExp("(^|&)" + params + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return "";
}
JS将时间转换为 刚刚
、几秒前
、几分钟前
、几小时前
、几天前
、几月前或按照传入格式显示
const formatPast = (date, type = "default", zeroFillFlag = true) => {
let countTime;
let time = new Date().getTime();
let afferentTime = new Date(date).getTime();
time = Number.parseInt(`${time - afferentTime}`);
if (time < 10000) {
return "刚刚";
} else if (time < 60000) {
countTime = Math.floor(time / 1000);
return `${countTime}秒前`;
} else if (time < 3600000) {
countTime = Math.floor(time / 60000);
return `${countTime}分钟前`;
} else if (time < 86400000) {
countTime = Math.floor(time / 3600000);
return `${countTime}小时前`;
} else if (time >= 86400000 && type == "default") {
countTime = Math.floor(time / 86400000);
if (countTime >= 365) {
return `${Math.floor(countTime / 365)}年前`;
}
if (countTime >= 30) {
return `${Math.floor(countTime / 30)}个月前`;
}
return `${countTime}天前`;
} else {
let Y = new Date(date).getFullYear();
let M = new Date(date).getMonth() + 1;
let zeroFillM = M > 9 ? M : "0" + M;
let D = new Date(date).getDate();
let zeroFillD = D > 9 ? D : "0" + D;
if (type == "-" || type == "/" || type == ".") {
return zeroFillFlag
? Y + type + zeroFillM + type + zeroFillD
: Y + type + M + type + D;
}
if (type == "年月日") {
return zeroFillFlag
? Y + type[0] + zeroFillM + type[1] + zeroFillD + type[2]
: Y + type[0] + M + type[1] + D + type[2];
}
if (type == "月日") {
return zeroFillFlag
? zeroFillM + type[0] + zeroFillD + type[1]
: M + type[0] + D + type[1]
}
if (type == "年") {
return Y + type
}
}
};
console.log(formatPast("2024-1-1 11:11:11"));
console.log(formatPast("2023-11-1 11:11:11"));
console.log(formatPast("2015-07-10 21:32:01"));
console.log(formatPast("2023-02-01 09:32:01", "-", false));
console.log(formatPast("2023.12.8 19:32:01", "/"));
console.log(formatPast("2023.12.8 19:32:01", "."));
console.log(formatPast("2023/5/10 11:32:01", "年月日"));
console.log(formatPast("2023/6/25 11:32:01", "月日", false));
console.log(formatPast("2023/8/08 11:32:01", "年"));
原链接:https://juejin.cn/post/7317704519160266779