美文网首页
判断重复组成新的一维组成三维数组

判断重复组成新的一维组成三维数组

作者: 糖醋里脊120625 | 来源:发表于2021-08-03 18:15 被阅读0次
    
          var dates = [
            "2020-08-01",
            "2020-08-02",
            "2020-08-05",
            "2021-07-01",
            "2021-07-02",
            "2021-08-10",
          ];
          var columns = [];
          dates.forEach((element) => {
            var elements = element.split("-");
            var yearColumn = this.getColumn(columns, elements[0]);
            if (!yearColumn) {
              yearColumn = { text: elements[0], children: [] };
            }
    
            var monthColumn = this.getColumn(yearColumn.children, elements[1]);
            if (!monthColumn) {
              monthColumn = { text: elements[1], children: [] };
            }
    
            var dayColumn = this.getColumn(monthColumn.children, elements[2]);
            if (!dayColumn) {
              dayColumn = { text: elements[2] };
            }
    
            this.addColumn(columns, yearColumn);
            this.addColumn(yearColumn.children, monthColumn);
            this.addColumn(monthColumn.children, dayColumn);
          });
          console.log(columns);
          return columns;
        },
          getColumn(columns, text) {
          for (let column of columns) {
            if (column.text && column.text == text) {
              return column;
            }
          }
        },
        addColumn(columns, newColumn) {
          var notContain = true;
          for (let column of columns) {
            if (column.text && column.text == newColumn.text) {
              notContain = false;
              return;
            }
          }
          if (notContain) {
            columns.push(newColumn);
          }
    
          return columns;
        },
    

    相关文章

      网友评论

          本文标题:判断重复组成新的一维组成三维数组

          本文链接:https://www.haomeiwen.com/subject/cbvevltx.html