工作中用到的工具函数整理

81 阅读1分钟

每一项都是对象的数组去重

用了reduce方法

原文: js让一个包含对象的数组去重_csdn_haow的博客-CSDN博客

 let arrObj = [
      {
        id: 1,
        name: "11"
      },
      {
        id: 2,
        name: "22"
      },
      {
        id: 3,
        name: "33"
      },
      {
        id: 1,
        name: "11"
      },
      {
        id: 5,
        name: "55"
      },
      {
        id: 9,
        name: "99"
      },
      {
        id: 9,
        name: "99"
      },
      {
        id: 6,
        name: "66"
      },
      {
        id: 5,
        name: "55"
      },
    ]
    let obj = {};
    function getArr(cur, next) {
      obj[next.id] ? "" : obj[next.id] = true && cur.push(next);
      return cur;
    }
    arrObj = arrObj.reduce(getArr, []);

时间格式转换

时间戳字符串转为日期格式

function timeConversion (time){
let date = new Date(Number(time));
let Y = date.getFullYear() + '-';
let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
return Y + M + D + h + m + s;
}