Data structure transformation
Preface
In production and life, many enterprises use sql database. In business, in order to reduce database access and data transmission to the front, I choose to change the data format Through the two-dimensional data format, it is converted into a tree structure. Since I have been in the position for half a year, and my business is relatively small, there is no guidance. Now I will share it for your reference and correction.
function sortData(Data){
var vernier,reData,X0,X1;
reData = [];
for(var i = 0;i<Data.length;i++){
vernier = Data[i].START_DATE_TIME.substring(0,10);
X0 = Data[i].START_DATE_TIME.substring(0,10);
X1 = Data[i].END_DATE_TIME || new Date();
if(typeof X1 === 'object'){
X1 = X1.getFullYear() + "-" + (X1.getMonth() + 1) + "-" + X1.getDate()
}
X1 = X1.substring(0,10);
var dateArr = inside(X0,X1,1)
for(var j=0;j<dateArr.length;j++){
vernier = dateArr[j];
!!reData[dateArr[j]]?reData[dateArr[j]].push(Data[i]):reData[dateArr[j]] = [Data[i]]
}
//debugger;
}
return reData
}
function inside(X0,X1,Interval){
var popArr = [];
var firstInt = new Date(X0);
var secondInt = (new Date(X1) - firstInt);
var days = Math.abs(secondInt / 86400000);
var freq = Math.ceil(days / Interval);
var i = 0;
var thenDate = 0;
while (i <= freq) {
thenDate = new Date(firstInt - 0 + i * Interval * 86400000);
thenDate = thenDate.getFullYear() + "-" + (thenDate.getMonth() + 1) + "-" + thenDate.getDate();
popArr.push(thenDate);
i++;
}
return popArr;
}
No time for the mind
People don't pretend to be B pseudo teenagers, by h.k
January 16, 2018
Looking at source code is a hobby, solving problems is a profession -- h.k