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;
},
网友评论