树–>数组
export function tree2Array(treeObj, rootid) {
let temp = []; // 设置临时数组,用来存放队列
let out = []; // 设置输出数组,用来存放要输出的一维数组
if (treeObj.length === 0) {
return treeObj;
}
temp = temp.concat(treeObj);
// 首先把根元素存放入out中
let pid = rootid;
const obj = deepCopy(treeObj);
const outObj = obj.map((item) => {
delete item.children;
return item;
});
out = out.concat(outObj);
// 对树对象进行广度优先的遍历
while (temp.length > 0) {
const first = temp.shift();
const { children } = first;
if (children && children.length > 0) {
pid = first.id;
const len = first.children.length;
for (let i = 0; i < len; i++) {
temp.push(children[i]);
// eslint-disable-next-line @typescript-eslint/no-shadow
const obj = deepCopy(children[i]);
obj.parentId = pid;
delete obj.children;
out.push(obj);
}
}
}
return out;
}
日期标准化
export function formatDate(date) {
let formatDateTime;
formatDateTime = new Date(date);
formatDateTime =
formatDateTime.getFullYear() +
'-' +
(formatDateTime.getMonth() + 1 > 9
? formatDateTime.getMonth() + 1
: '0' + (formatDateTime.getMonth() + 1)) +
'-' +
(formatDateTime.getDate() > 9 ? formatDateTime.getDate() : '0' + formatDateTime.getDate()) +
' ' +
(formatDateTime.getHours() > 9 ? formatDateTime.getHours() : '0' + formatDateTime.getHours()) +
':' +
(formatDateTime.getMinutes() > 9
? formatDateTime.getMinutes()
: '0' + formatDateTime.getMinutes()) +
':' +
(formatDateTime.getSeconds() > 9
? formatDateTime.getSeconds()
: '0' + formatDateTime.getSeconds());
return formatDateTime;
}
当前时间前半小时时间:
formatDate(Number(new Date()) - 0.5 * 60 * 60 * 1000);
排序:
.sort((a: any, b: any) => a.code - b.code)
时间戳:
new Date(start)).getTime()/1000
时间:
new Date(transdate(start) * 1000).toLocaleString().replace(/:\d{1,2}$/,’ '))
标题对象数组按属性排序
function compare(key) {
return function(value1, value2) {
var val1 = value1[key]
var val2 = value2[key]
return val2 - val1
}
}
this.partdata3 = data.part3.sort(compare('num'))