后台返回的数据JS根据日期分组,怎么处理?

比如有数据:
[
{time:'2017-12-07',title:'现在天气冷了'},
{time:'2017-12-07',title:'什么时候变热'},
{time:'2017-12-08',title:'什么时候过年'},
{time:'2017-12-08',title:'春节放假怎么安排'},
]
然后想要的效果是

https://img3.mukewang.com/5c7647cd0001f2ad05750291.jpg

尚方宝剑之说
浏览 596回答 1
1回答

眼眸繁星

var arr = [&nbsp; {time:'2017-12-07',title:'现在天气冷了'},&nbsp; {time:'2017-12-07',title:'什么时候变热'},&nbsp; {time:'2017-12-08',title:'什么时候过年'},&nbsp; {time:'2017-12-08',title:'春节放假怎么安排'},]var json = {}arr.forEach(function(v){&nbsp; &nbsp; if(json[v.time]){&nbsp; &nbsp; &nbsp; &nbsp; json[v.time].push(v.title);&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; json[v.time] = [v.title];&nbsp; &nbsp; }})console.log(json)//{"2017-12-07":["现在天气冷了","什么时候变热"],"2017-12-08":["什么时候过年","春节放假怎么安排"]}我只是写一个大概就是个双层循环var str = "";for(var key in json){&nbsp; &nbsp;str+=`<h1>${key}</h1>`;&nbsp; &nbsp;str+=`<div>`;&nbsp; &nbsp;for(var i=0;i<json[key].length;i++){&nbsp; &nbsp; &nbsp; &nbsp;str+=`<p>${json[key][i]}</p>`&nbsp; &nbsp;}&nbsp; &nbsp;str+=`</div>`;}console.log(str)/*<h1>2017-12-07</h1><div>&nbsp; &nbsp; <p>现在天气冷了</p>&nbsp; &nbsp; <p>什么时候变热</p></div><h1>2017-12-08</h1><div>&nbsp; &nbsp; <p>什么时候过年</p>&nbsp; &nbsp; <p>春节放假怎么安排</p></div>*/
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript