开发遇到个这个问题,后台返回的格式如下
但是设计稿如下:
后台没给我把总月数拿出来,我需要自己去遍历解决
,方法如下:
onLoad() {
let _this = this;
_this.$http.get('/pointRecords')
.then(res => {
if (_this.pageNum < res.data.pages) {
_this.list = _this.list.concat(res.data.list);
let times = [];
_this.list.forEach(item => {
times.push(_this.timeReset(item.createdDate));
});
times = [...new Set(times)];
let newArray = [];
times.forEach(item => {
let list = [];
_this.list.forEach(item2 => {
if (_this.timeReset(item) === _this.timeReset(item2.createdDate)) {
list.push(item2);
}
});
newArray.push(list);
});
_this.temporary = newArray;//因为是用vue写的,所以这边的temporary是用来循环的
}
;
}).
}
还有方法二:
var mouthArr = [
{
createdDate: '1'
}
];
var data = [
{
createdDate: '2018-03',
id: '1',
value: '1'
},
{
createdDate: '2018-04',
id: '2',
value: '2'
},
{
createdDate: '2018-03',
id: '3',
value: '3'
},
{
createdDate: '2018-06',
id: '4',
value: '4'
},
{
createdDate: '2018-08',
id: '5',
value: '5'
}
];
for (var j = 0; j < mouthArr.length; j++) {
if (data.length > 0) {
if (data[0].createdDate === mouthArr[j].createdDate) {
mouthArr[j].dataArr.push(data[0]);
data.splice(0, 1);
} else {
var obj = {};
obj.dataArr = [];
obj.createdDate = data[0].createdDate;
obj.dataArr.push(data[0]);
mouthArr.push(obj);
j = 0;
data.splice(0, 1);
}
}
}
mouthArr.splice(0, 1);
console.log(mouthArr)