3 回答

TA貢獻(xiàn)436條經(jīng)驗(yàn) 獲得超108個(gè)贊
<!DOCTYPE?html><html?lang="en"><head><meta?charset="UTF-8"><meta?name="viewport"?content="width=device-width,?initial-scale=1.0"><meta?http-equiv="X-UA-Compatible"?content="ie=edge"><title>Document</title><style>.record?{border:?1px?solid?#ccc;margin:?20px?0;}.record-header-time?{display:?block;line-height:?50px;background:?lightblue;}.record-item?{display:?flex;height:?100px;align-items:?center;border-bottom:?1px?solid?#ccc;}</style></head><body><div?id="app"></div><script>var?datas?=?[{"amount":?5000,"goodsInfo":?"翡翠城租金","tradeTime":?"2019-04-20"},{"amount":?100,"goodsInfo":?"萬象城租金","tradeTime":?"2019-04-20"},{"amount":?200,"goodsInfo":?"萬里長城租金","tradeTime":?"2019-04-20"},{"amount":?1200,"goodsInfo":?"五聯(lián)西苑租金","tradeTime":?"2019-04-25"},{"amount":?1100,"goodsInfo":?"新河苑租金","tradeTime":?"2019-04-25"},{"amount":?200,"goodsInfo":?"愛與家賓館","tradeTime":?"2019-04-30"},{"amount":?3800,"goodsInfo":?"丁香花園","tradeTime":?"2019-05-20"},{"amount":?2500,"goodsInfo":?"濱興小區(qū)","tradeTime":?"2019-05-28"},{"amount":?2800,"goodsInfo":?"濱苑小區(qū)","tradeTime":?"2019-06-08"}];function?appendItem(el,item){var?$item?=?document.createElement('div');var?$time?=?document.createElement('time');var?$info?=?document.createElement('span');var?$amount?=?document.createElement('span');$item.className?=?'record-item';$time.className?=?'record-item-time';$info.className?=?'record-item-goods';$amount.className?=?'record-item-amount';$time.textContent?=?item.tradeTime;$info.textContent?=?item.goodsInfo;$amount.textContent?=?item.amount;$item.appendChild($time);$item.appendChild($info);$item.appendChild($amount);el.appendChild($item);}function?appendHeaderTime(el,?time){var?$time?=?document.createElement('time');$time.className?=?'record-header-time';$time.textContent?=?time;el.appendChild($time);}var?frag?=?document.createDocumentFragment();for(var?i?=?0;i<datas.length;i++)?{var?$wrapper?=?document.createElement('div');var?v?=?datas[i];$wrapper.className?=?'record';appendHeaderTime($wrapper,?v.tradeTime);appendItem($wrapper,?v);for(var?j?=?i+1;j<datas.length;j++)?{var?o?=?datas[j];if(v.tradeTime?===?o.tradeTime)?{appendItem($wrapper,o);continue;}i?=?j-1;break;}frag.appendChild($wrapper);}document.getElementById('app').appendChild(frag);</script></body></html>
既然已經(jīng)有人說了如何提取同時(shí)間數(shù)據(jù),那我就不按題主要求來了,在不修改數(shù)據(jù)基礎(chǔ)上實(shí)現(xiàn)題主所要求的目的。一般來說后端提供的數(shù)據(jù)雖然沒有分時(shí)間給,但一般會(huì)按時(shí)間順序給。所以這里假設(shè)時(shí)間是有序的,那么就可以循環(huán)去比較時(shí)間然后去做對(duì)應(yīng)的DOM渲染。
方法有很多,這里使用循環(huán)是一個(gè)比較容易理解和想到的方案。也可以采用遞歸去處理。
關(guān)于代碼沒有美化問題,因?yàn)閺?fù)制到編輯器就這樣我就不換了,使用編輯器格式化一下即可。

TA貢獻(xiàn)5條經(jīng)驗(yàn) 獲得超0個(gè)贊
格式改為這樣吧:
newArray:{
????"2014-04":[
?????????????????{
????????? ? ?? ? ? ? ? ?"amount":5000,
????????? ? ? ? ? ? ? ? ?"goodsInfo":"翡翠城租金",
????????? ? ? ? ? ? ? ? "tradeTime":"2019-04-20"
????????? ? ? ? ?},
? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ?"amount":1200,
? ? ? ? ? ? ? ? ? ? ? "goodsInfo":"五聯(lián)西苑租金",
? ? ? ? ? ? ? ? ? ? ? ? "tradeTime":"2019-04-25"
? ? ? ? ? ? ? ? ?},
? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? "amount":200,
? ? ? ? ? ? ? ? ? ? ? ? ?"goodsInfo":"愛與家賓館",
? ? ? ? ? ? ? ? ? ? ? ? ? ?"tradeTime":"2019-04-30"
? ? ? ? ? ? ? ? ? ?}
????]
}
添加回答
舉報(bào)