最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angularjs - Looping through a List of objects in JavaScript - Stack Overflow

programmeradmin2浏览0评论

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 0
Add a ment  | 

2 Answers 2

Reset to default 3

Maybe 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);
    }
});
发布评论

评论列表(0)

  1. 暂无评论