I have an end point API. There are four objects with keys, id, group, name and date_modified. I am using AngularJS 1 to call the API and print the output. Now, there are few multiple entries, where group and name are same but date_modified is different.
To show the output, I want to filter it in a way that if group and name are same in an object then it will take the object where date_modified value is highest and ignore the others.
End point API JSON structure :-
[
{
"id": 1,
"group": "AB",
"name": "John",
"date_modified": "2018-01-30T12:01:47Z"
},
{
"id": 2,
"group": "BC",
"name": "Alex",
"date_modified": "2018-01-30T12:04:43Z"
},
{
"id": 3,
"group": "AB",
"name": "John",
"date_modified": "2018-01-29T12:00:02Z"
},
{
"id": 4,
"group": "CD",
"name": "Peter",
"date_modified": "2018-01-29T12:00:07Z"
}
]
services.js :-
app.factory('Instlistdata', function($resource) {
var list_data = 'http://127.0.0.1:8000/lists/?format=json' ;
return $resource(list_data, {}, {
query: {
method: 'GET',
isArray:true,
}
});
});
Controller.js :-
app.controller('dasboardController', ['$rootScope', '$scope', '$http', '$window', 'Instlistdata', function($rootScope, $scope, $http, $window, Instlistdata) {
$scope.lists = [];
Instlistdata.query({},function(data) {
$scope.lists = data;
console.log(data);
});
}]);
Now, it is giving me the JSON output in console. But I want filtered output, i.e
[
{
"id": 1,
"group": "AB",
"name": "John",
"date_modified": "2018-01-30T12:01:47Z"
},
{
"id": 2,
"group": "BC",
"name": "Alex",
"date_modified": "2018-01-30T12:04:43Z"
},
{
"id": 3,
"group": "CD",
"name": "Peter",
"date_modified": "2018-01-29T12:00:07Z"
}
]
"date_modified": "2018-01-30T12:01:47Z" is higher that the "date_modified": "2018-01-29T12:00:02Z" where "group" : "AB" and "name" : "John" . You can see we ignored the other one.