工具函数集合
工具函数集合
工具函数集合
工具函数集合
递归查找树形结构符合条件的节点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 递归查找对应的name
export const findElements = (tree, item, condition) => {
let results = '';
function traverse(node) {
if (condition(node, item)) {
results = node.name;
return true;
}
if (node.children && node.children.length > 0) {
for (const child of node.children) {
traverse(child);
}
}
}
for (const root of tree) {
if(traverse(root)){break}
}
return results;
}
通过第三级获取级联路径集合(通过区查询省市区code集合)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 递归处理回显省市区
changeDetSelect(key, treeData) {
let arr = []; // 在递归时操作的数组
let returnArr = []; // 存放结果的数组
let depth = 0; // 定义全局层级
// 定义递归函数
function childrenEach(childrenData, depthN) {
for (let j = 0; j < childrenData.length; j++) {
depth = depthN; // 将执行的层级赋值 到 全局层级
arr[depthN] = childrenData[j].value;
if (childrenData[j].value === key) {
returnArr = arr.slice(0, depthN + 1); //将目前匹配的数组,截断并保存到结果数组,
break;
} else {
if (childrenData[j].children) {
depth++;
childrenEach(childrenData[j].children, depth);
}
}
}
return returnArr;
}
return childrenEach(treeData, depth);
},
倒计时 - 指定分钟进行倒计时
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 倒计时 - 指定分钟进行倒计时
export function timeCountDown(countDownConfig: TimeCountDownConfig) {
countDownConfig.onCountDownStart && countDownConfig.onCountDownStart();
let maxTime = countDownConfig.minute * 60;
let timer = setInterval(() => {
if (maxTime > 0) {
--maxTime;
let minutes: number = Math.floor(maxTime / 60);
let seconds: number = Math.floor(maxTime % 60);
let timeStr: string = String(minutes).padStart(2, '0') + ":" + String(seconds).padStart(2, '0');
countDownConfig.onCountDown && countDownConfig.onCountDown(timeStr);
} else {
clearInterval(timer)
countDownConfig.onCountDownEnd && countDownConfig.onCountDownEnd();
}
}, 1000)
return timer;
}
倒计时 - 截止时间进行倒计时
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 倒计时 - 截止时间进行倒计时
export function deadLinetCountDown(countDownConfig: deadLinetCountDownConfig) {
countDownConfig.onCountDownStart && countDownConfig.onCountDownStart();
// 截止时间
const deadLine = new Date(countDownConfig.time).getTime();
let timer = setInterval(() => {
// 获取当前时间
const nowTime = new Date().getTime();
//倒计时结束时间 - 当前时间 = 倒计时时间
//再将倒计时时间转换为秒
const countdownTime = (deadLine - nowTime) / 1000;
if (countdownTime > 0) {
let minutes: number = Math.floor(countdownTime / 60);
let seconds: number = Math.floor(countdownTime % 60);
let timeStr: string = String(minutes).padStart(2, '0') + ":" + String(seconds).padStart(2, '0');
countDownConfig.onCountDown && countDownConfig.onCountDown(timeStr);
} else {
clearInterval(timer)
countDownConfig.onCountDownEnd && countDownConfig.onCountDownEnd();
}
}, 1000)
return timer;
}
全局通用的数据类型判断方法
1
2
3
4
5
6
7
8
function getType(obj){
let type = typeof obj;
if (type !== "object") { // 先进行typeof判断,如果是基础数据类型,直接返回
return type;
}
// 对于typeof返回结果是object的,再进行如下的判断,正则返回结果
return Object.prototype.toString.call(obj).replace(/^\[object (\S+)\]$/, '$1');
}
使用如下
1
2
3
4
5
6
7
8
getType([]) // "Array" typeof []是object,因此toString返回
getType('123') // "string" typeof 直接返回
getType(window) // "Window" toString返回
getType(null) // "Null"首字母大写,typeof null是object,需toString来判断
getType(undefined) // "undefined" typeof 直接返回
getType() // "undefined" typeof 直接返回
getType(function(){}) // "function" typeof能判断,因此首字母小写
getType(/123/g) //"RegExp" toString返回
获取两日期之间日期列表函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//获取两日期之间日期列表函数
getDiffDate = (stime, etime) => {
stime = moment(stime).format('YYYY-MM-DD');
etime = moment(etime).format('YYYY-MM-DD');
//初始化日期列表,数组
let diffDate = [];
let i = 0;
//开始日期小于等于结束日期,并循环
while (stime <= etime) {
diffDate[i] = stime;
//获取开始日期时间戳
let stime_ts = new Date(stime).getTime();
// console.log('当前日期:'+stime +'当前时间戳:'+stime_ts);
//增加一天时间戳后的日期
let next_date = stime_ts + (24 * 60 * 60 * 1000);
//拼接年月日,这里的月份会返回(0-11),所以要+1
let next_dates_y = new Date(next_date).getFullYear() + '-';
let next_dates_m = (new Date(next_date).getMonth() + 1 < 10) ? '0' + (new Date(next_date).getMonth() + 1) + '-' : (new Date(next_date).getMonth() + 1) + '-';
let next_dates_d = (new Date(next_date).getDate() < 10) ? '0' + new Date(next_date).getDate() : new Date(next_date).getDate();
stime = next_dates_y + next_dates_m + next_dates_d;
//增加数组key
i++;
}
return diffDate;
}
树形结构数据筛选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const filterData = (data, keyword) => {
return data.filter(node => {
// 判断当前节点是否匹配筛选条件
if (node.name.includes(keyword)) {
return true;
}
// 递归过滤子节点
if (node.children && node.children.length) {
node.children = filterData(node.children, keyword);
return node.children.length > 0;
}
return false;
});
};
本文由作者按照 CC BY 4.0 进行授权