![](/img/trans.png)
[英]Looping through a list of Java objects in JavaScript with Thymeleaf
[英]Looping through a List of objects in JavaScript
我想遍历从服务器上的REST服务收到的学生列表。 此列表包含适用于某个部分的学生的对象。 每个对象都有firstName,lastName,Student ID和许多其他属性,特别是名为isAbsent
的属性。 它是一个布尔值,如果学生缺席则为true,如果学生缺席则为false。 我想在另一个字符串数组中存储缺席的学生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);
}
}
ps不在数组中使用for..in
。 它将显示索引而不是值。 它不像C#。
根据您需要支持的浏览器(例如IE> = 9),我建议使用较新的foreach
结构 。 我发现它更易于使用:
$scope.studentList.forEach(function(value, index, array) {
if (value.isAbsent === true)
{
$scope.selection.push(value.id);
console.log($scope.selection);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.