做前后端分離,現(xiàn)有如下數(shù)組按照updated_at倒序,根據(jù)year做第一級,month做第二級,拼裝成多維數(shù)組,原先是在php后端進行排序處理,但是通過json傳遞到前端變成無序,現(xiàn)在想通過js,該如何處理?
[
{
"id": 15,
"year": "2018",
"month": "03",
"updated_at": "2018-03-01 15:09:59"
},
{
"id": 14,
"year": "2018",
"month": "02",
"updated_at": "2018-02-24 21:39:53"
},
{
"id": 13,
"year": "2017",
"month": "12",
"updated_at": "2017-12-02 16:45:03"
},
{
"id": 5,
"year": "2017",
"month": "11",
"updated_at": "2017-11-10 17:32:11"
},
{
"id": 7,
"year": "2017",
"month": "11",
"updated_at": "2017-11-07 17:26:09"
},
{
"id": 9,
"year": "2017",
"month": "10",
"updated_at": "2017-10-23 19:08:12"
},
{
"id": 8,
"year": "2017",
"month": "10",
"updated_at": "2017-10-20 20:13:07"
},
{
"id": 10,
"year": "2017",
"month": "09",
"updated_at": "2017-09-23 19:05:52"
},
{
"id": 4,
"year": "2017",
"month": "09",
"updated_at": "2017-09-18 17:32:49"
},
{
"id": 3,
"year": "2017",
"month": "07",
"updated_at": "2017-07-22 17:34:20"
},
{
"id": 6,
"year": "2017",
"month": "07",
"updated_at": "2017-07-02 17:28:49"
},
{
"id": 12,
"year": "2017",
"month": "06",
"updated_at": "2017-06-15 15:31:18"
},
{
"id": 11,
"year": "2017",
"month": "06",
"updated_at": "2017-06-11 15:32:16"
}
]
期望所得數(shù)據(jù)如下
"2018": {
"03": [{
"id": 15,
"updated_at": "2018-03-01"
}],
"02": [{
"id": 14,
"updated_at": "2018-02-24"
}]
},
"2017": {
"12": [{
"id": 13,
"updated_at": "2017-12-02"
}],
"11": [{
"id": 5,
"updated_at": "2017-11-10"
}, {
"id": 7,
"updated_at": "2017-11-07"
}],
"10": [{
"id": 9,
"updated_at": "2017-10-23"
}, {
"id": 8,
"updated_at": "2017-10-20"
}],
"09": [{
"id": 10,
"updated_at": "2017-09-23"
}, {
"id": 4,
"updated_at": "2017-09-18"
}],
"07": [{
"id": 3,
"updated_at": "2017-07-22"
}, {
"id": 6,
"updated_at": "2017-07-02"
}],
"06": [{
"id": 12,
"updated_at": "2017-06-15"
}, {
"id": 11,
"updated_at": "2017-06-11"
}]
}
}請補充說明期望的結果(給出例子)
也就是說:
let years = {}
years['2018'] = 'haha'
years['2017'] = 'heihei'
console.log(Object.keys(years)) // 不保證是 ['2018', '2017'],有可能是 ['2017', '2018']
console.log(years) // 不保證是 {2018: 'haha', 2017: 'heihei'},有可能是 {2017: 'heihei', 2018: 'haha'}
在此基礎之上,如果仍要做的話,可以按照以下步驟:
將原數(shù)組按照 year->month 的順序排序
year month 正是 updated_at 中的年月的話,按照 updated_at 倒序其實就已經(jīng)按照 year->month 排好了data.sort(function (d1, d2) => {
if (d1.year > d2.year) return -1
if (d1.year < d2.year) return 1
return (+d2.month) - (+d1.month)
})
此時遍歷 data 即可
let years = {}
data.forEach(function (d) => {
if (!years[d.year]) years[d.year] = {}
let thisYear = years[d.year]
if (!thisYear[d.month]) thisYear[d.month] = []
thisYear[d.month].push({
id: d.id,
updated_at: d.updated_at
})
})
consol.log(years) // 就是你要的結果了
整理下代碼:
const result = data
.sort((d1, d2) => {
if (d1.year > d2.year) return -1
if (d1.year < d2.year) return 1
return (+d2.month) - (+d1.month)
})
.reduce((years, {id, year, month, updated_at}) => {
const thisYear = (years[year] = years[year] || {})
const thisMonth = (thisYear[month] = thisYear[month] || [])
thisMonth.push({id, updated_at})
return acc
}, {})
const result = data
.sort((d1, d2) => {
if (d1.year > d2.year) return -1
if (d1.year < d2.year) return 1
return (+d2.month) - (+d1.month)
})
.reduce((years, {id, year, month, updated_at}) => {
if (!years.has(year)) years.set(year, new Map())
const thisYear = years.get(year)
if (!thisYear.has(month)) thisYear.set(month, [])
const thisMonth = thisYear.get(month)
thisMonth.push({id, updated_at})
return acc
}, new Map())
希望對你有幫助
data.sort((a,b)=>b.updated_at.replace(/[-\s:]/g,"")-a.updated_at.replace(/[-\s:]/g,""));
var arr = {};
//data.map((item)=>{
// arr[item.year]?arr[item.year].push(item):arr[item.year]=[item]
//})
//修改
data.map((item)=>{
if(!arr[item.year]){
arr[item.year]={}
}
arr[item.year][item.month]?arr[item.year][item.month].push(item):arr[item.year][item.month]=[item]
})北大青鳥APTECH成立于1999年。依托北京大學優(yōu)質雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
達內教育集團成立于2002年,是一家由留學海歸創(chuàng)辦的高端職業(yè)教育培訓機構,是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
北大課工場是北京大學校辦產業(yè)為響應國家深化產教融合/校企合作的政策,積極推進“中國制造2025”,實現(xiàn)中華民族偉大復興的升級產業(yè)鏈。利用北京大學優(yōu)質教育資源及背
博為峰,中國職業(yè)人才培訓領域的先行者
曾工作于聯(lián)想擔任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責任公司從事總經(jīng)理職務負責iOS教學及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術, 熟練的跨平臺面向對象開發(fā)經(jīng)驗,技術功底深厚。 授課風格 授課風格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術顧問,美國Dachieve 系統(tǒng)架構師,美國AngelEngineers Inc. 系統(tǒng)架構師。