json数组按照日期先后排序
程序员文章站
2022-05-18 08:59:14
var allMyApp = [ {"startDate": "2018-07-07 12:30:00",'name':'aa'}, {"startDate": "2018-07-09 12:30:00",'name':'bb'}, {"startDate": "2018-07-05 12:30:0... ......
var allMyApp = [ {"startDate": "2018-07-07 12:30:00",'name':'aa'}, {"startDate": "2018-07-09 12:30:00",'name':'bb'}, {"startDate": "2018-07-05 12:30:00",'name':'cc'}, {"startDate": "2018-07-01 12:30:00",'name':'dd'} ]; var newArr = []; for(var p = 0; p < allMyApp.length; p++){ newStartDate = (allMyApp[p].startDate).replace(new RegExp("-", "gm"), "/"); startHaoMiao = (new Date(newStartDate)).getTime(); //得到毫秒数 newArr.push(allMyApp[p]) newArr[p].timeStartApp = startHaoMiao; } console.log(newArr); var arr = newArr.sort(sortDate);
//arr就是最终的排序后的数组 console.log(arr); function sortDate(a,b){ return a.timeStartApp-b.timeStartApp }
上一篇: js时间与毫秒互相转换