Reputation: 1171
I want to filter object within some year range.
The object I have looks like this:
students = [ {studentId: 1,
studentName: 'Alex',
studentClass: 1,
studentBirth: 1345262400000},
{studentId: 2,
studentName: 'Bob',
studentClass: 1,
studentBirth: 1345608000000},
...
];
And I want to find some students that birth year > 1994 && birth year < 2000. Also, I want to narrow down based on student's class.
I wrote code like this:
var targetStudent = {};
targetStudent['studentClass'] = 1;
students = $filter('filter')(students, targetStudent);
With above code, I can find students whose class is 1, but I don't know how to filter out based on year.
I could write something like:
students = $filter('filter')(students, function(student) {
if(new Date(student.studentBirth).getFullYear() < 2000 && new Date(student.studentBirth).getFullYear() > 1994)
return true;
else
return false;
});
But I want to create an object to find students. Is it possible?
Upvotes: 1
Views: 59
Reputation: 1893
You are actually very close to answering your own question. When using a custom filter function you can return the object and you will get an array of objects back.
students = $filter('filter')(students, function(student) {
if(new Date(student.studentBirth).getFullYear() < 2000 && new Date(student.studentBirth).getFullYear() > 1994) {
return student;
}
});
Unless this is only being used once, and even then, I'd go with something more reusable:
$scope.getStudentsForRange = function(studentList, fromYear, toYear) {
return $filter('filter')(studentList, function(student) {
var studentBirthYear = new Date(student.studentBirth).getFullYear();
if(studentBirthYear < toYear && studentBirthYear > fromYear) {
return student;
}
};
};
Which you can use like so: $scope.getStudentsForRange(students, 1994, 2000)
This has the added bonus of only creating a new Date object once, keeping things DRY.
EDIT
It sounds like you want a custom filter.
angular.module('rootApp', []).filter('studentFilter', function() {
return function(students, studentFilter) {
return $filter('filter')(students, function(student) {
var birthYear = new Date(student.studentBirth).getFullYear();
if(student.studentClass === studentFilter.studentClass && birthYear < studentFilter.toYear && studentFilter.birthYear > fromYear) {
return student;
}
};
};
});
Which can be used in a template: {{ students | studentFilter:{studentClass: 1, fromYear: 1994, toYear: 2000} }}
.
Or inside a controller/directive:
var targetStudent = {
studentClass: 1,
fromYear: 1994,
toYear: 2000
};
students = $filter('studentFilter')(students, targetStudent);
Upvotes: 1