const bookData = {
"bookCount": 6,
"folderCount": 0,
"folderPrefix": "https://t.flipbuilder.com/fh_wkdc/folders/cover/",
"books": [
{
"bookId": 16022573,
"title": "圣帝雅门窗(封面封底为单面,其余为双面11111111111111111111111)",
"newTime": "1679034615775",
"number": "96362143",
"isPublic": 1,
"old_isPublic": 1,
"seo_boost": 1,
"templateId": -1,
"price": "5.00",
"authorId": 46391074,
"ownerId": 46391074,
"url": "https://t.flipbuilder.com/fh_wkdc/xvno/",
"desc": "",
"bStatus": 1,
"label": 0,
"updateTime": "1682303154648",
"pages": 52,
"bLink": "xvno",
"bType": 1,
"auditStatus": 1,
"auditOpinion": "",
"loadMode": 2,
"folderId": 0,
"folderCode": "|0|5|3|",
"img": "https://t.flipbuilder.com/fh_wkdc/xvno/files/shot.jpg",
"web_url": "https://dev.fliphtml5.com/fh_wkdc/xvno/",
"book_url": "https://t.flipbuilder.com/fh_wkdc/xvno/",
"domain_url": "",
"qr_code": "https://t.flipbuilder.com/fh_wkdc/xvno/files/extfile/QRURL.png",
"wx_code": "https://t.flipbuilder.com/fh_wkdc/xvno/files/extfile/WX_QRURL.png?x-oss-process=image/resize,m_fixed,h_250,w_250",
"shareIcon": "https://t.flipbuilder.com/fh_wkdc/xvno/files/",
"errorCode": ""
},
{
"bookId": 38077751,
"title": "11111111111111111111111111",
"newTime": "1664328769960",
"number": "32989413",
"isPublic": 1,
"old_isPublic": 1,
"seo_boost": 1,
"templateId": -1,
"price": "0.00",
"authorId": 46391074,
"ownerId": 46391074,
"url": "https://t.flipbuilder.com/fh_wkdc/ukee/",
"desc": "",
"bStatus": 1,
"label": 0,
"updateTime": "1677575749740",
"pages": 16,
"bLink": "ukee",
"bType": 1,
"auditStatus": 1,
"auditOpinion": "",
"loadMode": 2,
"folderId": 0,
"folderCode": "|0|",
"img": "https://t.flipbuilder.com/fh_wkdc/ukee/files/shot.jpg",
"web_url": "https://dev.fliphtml5.com/fh_wkdc/ukee/",
"book_url": "https://t.flipbuilder.com/fh_wkdc/ukee/",
"domain_url": "",
"qr_code": "https://t.flipbuilder.com/fh_wkdc/ukee/files/extfile/QRURL.png",
"wx_code": "https://t.flipbuilder.com/fh_wkdc/ukee/files/extfile/WX_QRURL.png?x-oss-process=image/resize,m_fixed,h_250,w_250",
"shareIcon": "https://t.flipbuilder.com/fh_wkdc/ukee/files/",
"errorCode": ""
},
{
"bookId": 72794599,
"title": "111111",
"newTime": "1662426272343",
"number": "30516425",
"isPublic": 1,
"old_isPublic": 1,
"seo_boost": 1,
"templateId": -1,
"price": "0.00",
"authorId": 46391074,
"ownerId": 46391074,
"url": "https://t.flipbuilder.com/fh_wkdc/hltl/",
"desc": "111111",
"bStatus": 1,
"label": 0,
"updateTime": "1679040892677",
"pages": 28,
"bLink": "hltl",
"bType": 1,
"auditStatus": 1,
"auditOpinion": "",
"loadMode": 2,
"folderId": 5,
"folderCode": "|0|5|",
"img": "https://t.flipbuilder.com/fh_wkdc/hltl/files/shot.jpg",
"web_url": "https://dev.fliphtml5.com/fh_wkdc/hltl/",
"book_url": "https://t.flipbuilder.com/fh_wkdc/hltl/",
"domain_url": "",
"qr_code": "https://t.flipbuilder.com/fh_wkdc/hltl/files/extfile/QRURL.png",
"wx_code": "https://t.flipbuilder.com/fh_wkdc/hltl/files/extfile/WX_QRURL.png?x-oss-process=image/resize,m_fixed,h_250,w_250",
"shareIcon": "https://t.flipbuilder.com/fh_wkdc/hltl/files/",
"errorCode": ""
},
{
"bookId": 39376577,
"title": "111111",
"newTime": "1662371929105",
"number": "22623349",
"isPublic": 1,
"old_isPublic": 1,
"seo_boost": 1,
"templateId": -1,
"price": "0.00",
"authorId": 46391074,
"ownerId": 46391074,
"url": "https://t.flipbuilder.com/fh_wkdc/aoyo/",
"desc": "111111",
"bStatus": 1,
"label": 0,
"updateTime": "1679040892677",
"pages": 28,
"bLink": "aoyo",
"bType": 1,
"auditStatus": 1,
"auditOpinion": "",
"loadMode": 2,
"folderId": 5,
"folderCode": "|0|5|",
"img": "https://t.flipbuilder.com/fh_wkdc/aoyo/files/shot.jpg",
"web_url": "https://dev.fliphtml5.com/fh_wkdc/aoyo/",
"book_url": "https://t.flipbuilder.com/fh_wkdc/aoyo/",
"domain_url": "",
"qr_code": "https://t.flipbuilder.com/fh_wkdc/aoyo/files/extfile/QRURL.png",
"wx_code": "https://t.flipbuilder.com/fh_wkdc/aoyo/files/extfile/WX_QRURL.png?x-oss-process=image/resize,m_fixed,h_250,w_250",
"shareIcon": "https://t.flipbuilder.com/fh_wkdc/aoyo/files/",
"errorCode": ""
},
{
"bookId": 60725670,
"title": "QTP_PAM11111111_复制",
"newTime": "1656992904266",
"number": "95120514",
"isPublic": 1,
"old_isPublic": 1,
"seo_boost": 1,
"templateId": -1,
"price": "0.00",
"authorId": 46391074,
"ownerId": 46391074,
"url": "https://t.flipbuilder.com/fh_wkdc/qbii/",
"desc": "QTP_PAM",
"bStatus": 1,
"label": 0,
"updateTime": "1679040892677",
"pages": 20,
"bLink": "qbii",
"bType": 1,
"auditStatus": 0,
"auditOpinion": "",
"loadMode": 2,
"folderId": 5,
"folderCode": "|0|8|",
"img": "https://t.flipbuilder.com/fh_wkdc/qbii/files/shot.jpg",
"web_url": "https://dev.fliphtml5.com/fh_wkdc/qbii/",
"book_url": "https://t.flipbuilder.com/fh_wkdc/qbii/",
"domain_url": "",
"qr_code": "https://t.flipbuilder.com/fh_wkdc/qbii/files/extfile/QRURL.png",
"wx_code": "https://t.flipbuilder.com/fh_wkdc/qbii/files/extfile/WX_QRURL.png?x-oss-process=image/resize,m_fixed,h_250,w_250",
"shareIcon": "https://t.flipbuilder.com/fh_wkdc/qbii/files/",
"errorCode": ""
},
{
"bookId": 33842564,
"title": "QTP_PAM11111111_复制_复制",
"newTime": "1656747658079",
"number": "28886129",
"isPublic": 1,
"old_isPublic": 1,
"seo_boost": 1,
"templateId": -1,
"price": "0.00",
"authorId": 46391074,
"ownerId": 46391074,
"url": "https://t.flipbuilder.com/fh_wkdc/daxg/",
"desc": "222222",
"bStatus": 1,
"label": 0,
"updateTime": "1677575739230",
"pages": 20,
"bLink": "daxg",
"bType": 1,
"auditStatus": 0,
"auditOpinion": "",
"loadMode": 2,
"folderId": 12,
"folderCode": "|0|12|",
"img": "https://t.flipbuilder.com/fh_wkdc/daxg/files/shot.jpg",
"web_url": "https://dev.fliphtml5.com/fh_wkdc/daxg/",
"book_url": "https://t.flipbuilder.com/fh_wkdc/daxg/",
"domain_url": "",
"qr_code": "https://t.flipbuilder.com/fh_wkdc/daxg/files/extfile/QRURL.png",
"wx_code": "https://t.flipbuilder.com/fh_wkdc/daxg/files/extfile/WX_QRURL.png?x-oss-process=image/resize,m_fixed,h_250,w_250",
"shareIcon": "https://t.flipbuilder.com/fh_wkdc/daxg/files/",
"errorCode": ""
}
],
"folders": [],
"orderSort": {
"newTime": "desc"
},
"parents": [],
"folderBookCount": 6,
"folderTitle": "根目录",
"lastTime": 1685005082952
}
const folderData = {
"bookCount": 0,
"folderCount": 1,
"folderPrefix": "https://t.flipbuilder.com/fh_wkdc/folders/cover/",
"books": [],
"folders": [
{
"id": 59,
"ownerId": 46391074,
"categoryId": 0,
"coverimg": "",
"description": "",
"newTime": "1660529929956",
"parentCode": "|0|",
"title": "1111111111111111111111111111111",
"updateTime": "1672282125643",
"authorId": 46391074,
"isdefault": 0,
"isPayRead": 0
}
],
"orderSort": {
"newTime": "desc"
},
"parents": [],
"folderBookCount": 1,
"folderTitle": "根目录",
"lastTime": 1685005083573
}
const list = [].concat(folderData.folders.map(item => ({...item, sortTime: item.newTime})), bookData.books.map(item => ({...item, sortTime: item.newTime})))
list.sort((a, b) => {
const aCode = (a.parentCode || a.folderCode)
const bCode = (b.parentCode || b.folderCode)
return aCode.localeCompare(bCode)
})
const treeList = list.reduce((res, cur, i) => {
const code = cur.parentCode || cur.folderCode
const curArr = res.length && res[res.length - 1]
const lastFirstItem = res.length && curArr[0]
const lfCode = res.length && (lastFirstItem.parentCode || lastFirstItem.folderCode)
if (res.length && curArr && lastFirstItem && lfCode && code === lfCode) {
curArr.push(cur)
// sortTime 升序,或者直接 a.sortTime - b.sortTime 也行
curArr.sort((a, b) => new Date(Number(a.sortTime)) - new Date(Number(b.sortTime)))
// 文件夹靠后
// curArr.sort((a, b) => Boolean(a.id) - Boolean(b.id))
} else {
res.push([cur])
}
return res
}, [])
// sortTime 升序,或者直接 a.sortTime - b.sortTime 也行
treeList.sort((a, b) => new Date(Number(a[0].sortTime)) - new Date(Number(b[0].sortTime)))
console.log(treeList)
console