I want to loop through the student list which I received from a REST service located on a server. this list contains objects for students enrolled in a section. Each object has firstName, lastName, student ID and many other attributes, specifically an attribute called isAbsent
. Its a Boolean value where it has true if the student is absent and false if the student is present in not absent. I want to store the students IDs who are absent (have isAbsent=true
) in another String Array.
I tried this :
{
//this array will store the IDs of students who are absent.
$scope.selection = [];
//$scope.studentList is the list of students for CRN=X and Date=Y
for (id in $scope.studentList) {
if ($scope.studentList.isAbsent === true) {
$scope.selection.push($scope.studentList.id);
console.log($scope.selection);
}
}
}
This code doesn't execute. I have no clue why, I guess the problem in the loop structure. Any help?
I want to loop through the student list which I received from a REST service located on a server. this list contains objects for students enrolled in a section. Each object has firstName, lastName, student ID and many other attributes, specifically an attribute called isAbsent
. Its a Boolean value where it has true if the student is absent and false if the student is present in not absent. I want to store the students IDs who are absent (have isAbsent=true
) in another String Array.
I tried this :
{
//this array will store the IDs of students who are absent.
$scope.selection = [];
//$scope.studentList is the list of students for CRN=X and Date=Y
for (id in $scope.studentList) {
if ($scope.studentList.isAbsent === true) {
$scope.selection.push($scope.studentList.id);
console.log($scope.selection);
}
}
}
This code doesn't execute. I have no clue why, I guess the problem in the loop structure. Any help?
Share Improve this question edited Jun 7, 2014 at 16:08 iamruss 8125 silver badges11 bronze badges asked Jun 7, 2014 at 15:39 Ali KhalilAli Khalil 931 gold badge2 silver badges11 bronze badges 02 Answers
Reset to default 3Maybe this will help?
for (var i=0;i<$scope.studentList.length;i++)
{
if ($scope.studentList[i].isAbsent === true)
{
$scope.selection.push($scope.studentList[i].id);
console.log($scope.selection);
}
}
p.s. don't use for..in
with arrays. It will display an index instead of a value. It's not like C#.
Depending on what browsers you need to support (e.g. IE >= 9), I'd suggest the newer foreach
construct. I find it easier to use:
$scope.studentList.forEach(function(value, index, array) {
if (value.isAbsent === true)
{
$scope.selection.push(value.id);
console.log($scope.selection);
}
});