小可爱的整理啊

树–>数组

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'))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值