gpt4 book ai didi

javascript - 遍历 JavaScript 中的对象列表

转载 作者:行者123 更新时间:2023-11-30 08:02:43 29 4
gpt4 key购买 nike

我想遍历从位于服务器上的 REST 服务收到的学生列表。此列表包含在某个部分注册的学生的对象。每个对象都有名字、姓氏、学生 ID 和许多其他属性,特别是一个名为 isAbsent 的属性。它是一个 bool 值,如果学生缺席则为真,如果学生在场而不是缺席则为假。我想将缺席的学生 ID(isAbsent=true)存储在另一个字符串数组中。

我试过这个:

{ 
//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);
}
}
}

此代码不执行。我不知道为什么,我猜是循环结构中的问题。有帮助吗?

最佳答案

也许这会有帮助?

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);
}
}

附注不要对数组使用 for..in。它将显示一个索引而不是一个值。它不像 C#。

关于javascript - 遍历 JavaScript 中的对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24098824/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com