1 回答
TA贡献1871条经验 获得超13个赞
最终把数据格式转换成:
newData = [
{
time: '2017/06',
items: [
{ time: '2017/06/23', money: '3000', type: 'RMB'},
{ time: '2017/06/24', money: '4000', type: 'RMB'},
]
},
{
time: '2017/07',
items: [
{ time: '2017/07/23', money: '3000', type: 'RMB'},
{ time: '2017/07/24', money: '4000', type: 'RMB'},
]
},
];
然后使用两个ng-repeat渲染。
至于思路的话:
先转换成一个对象:
obj = {
'2016/06': [
{ time: '2017/06/23', money: '3000', type: 'RMB'},
{ time: '2017/06/24', money: '4000', type: 'RMB'},
],
'2016/07': [
{ time: '2017/07/23', money: '3000', type: 'RMB'},
{ time: '2017/07/24', money: '4000', type: 'RMB'},
]
}
然后遍历对象,转换成数组。
const data = [
{ time: '2016/06/23', money: '1000', type: 'RMB' },
{ time: '2016/06/24', money: '1200', type: 'RMB' },
{ time: '2016/07/12', money: '1200', type: 'RMB' },
{ time: '2016/07/15', money: '1200', type: 'RMB' },
];
const obj = _.groupBy(data, item => item.time.substr(0, 7)); // 我这里使用了lodash,自行遍历数组也是一样的
const newData = Object.keys(obj).map(time => ({ time, items: obj[time] }));
console.log(newData, 2);
添加回答
举报