我的json文件如下所示。
{
"components":{
"record1":
[
{
"date":1419038000,
"name":"Vinay",
"status":"false"
},
{
"date":1419034102,
"name":"Ajay",
"status":"true"
}
],
"record2":[
{
"date":1619037000,
"name":"Soumya",
"status":"true"
},
{
"date":1419038000,
"name":"Savio",
"status":"false"
}
]
}
}
这是我的js文件:
var app=angular.module("myApp",['angular.filter']);
app.controller("Control",function($scope,$http){
$scope.output=[];
$http.get('myData.json').
success(function(data){
$scope.tableData = data.components;
for(var i in Object.keys($scope.tableData)){
for(var j in $scope.tableData[Object.keys($scope.tableData)[i]]){
$scope.output.push($scope.tableData[Object.keys($scope.tableData)[i]][j]);
}
}
console.log($scope.output);
});
});
我想仅根据给定时间戳到表中的日期将两个对象数组中的上述数据分组。我已附上下面图片的链接。 enter image description here
答案 0 :(得分:1)
尝试如下内容:
// create object whose keys are dates
// produces {'2012-11-11':{date: DateObj, items:[{name:...},{..}]}, '2014-09-02':{date:....}, ...}
var dateGroupObj = Object.keys(data.components).reduce(function(a, c){
data.components[c].forEach(function(item){
var date= new Date(item.abc*1000);
var dateStr = date.toISOString().slice(0,10);
a[dateStr] = a[dateStr] ? a[dateStr] : {date: new Date(dateStr), items:[]};
a[dateStr].items.push(item);
});
return a;
},{});
// map above object to array to allow sorting, filtering etc
$scope.dateGroups = Object.keys(dateGroupObj).map(function(key){
return dateGroupObj[key];
});
查看
<table>
<tbody ng-repeat="group in dateGroups | orderBy:'date'">
<tr ng-repeat="item in group.items">
<td rowspan="{{group.items.length+1}}" ng-if="$index==0">{{group.date|date}}</td>
<td>{{item.name}}</td>
<td>{{item.status}}</td>
</tr>
</tbody>
</table>
的 DEMO 强>