我有這個對象數組,我想計算學生根據某些條件花費在特定主題上的時間。如何找到一個數組中的對象的總和
我有這段代碼來識別主題: 但我很努力地找到這些特定主題的總分鐘。
$scope.total = 0;
$scope.found = [];
angular.forEach($scope.all, function(value, index) {
angular.forEach(value.time, function (item, index) {
$scope.total += value.time[index].minutes;
if (item.subject === "english" || item.subject === "maths"
&& $scope.total === 150) {
$scope.found.push(value);
}
});
});
"students": [
{
"id": 1,
"name": "Natalie",
"gender": "female",
"time": [
{
"subject": "geography",
"minutes": 100
},{
"subject": "english",
"minutes": 20
},{
"subject": "maths",
"minutes": 760
}
]
},{
"id": 2,
"name": "John",
"gender": "male",
"time": [
{
"subject": "spanish",
"minutes": 450
},{
"subject": "maths",
"minutes": 900
},{
"subject": "geography",
"minutes": 200
}
]
}
]
你沒有張貼有效的JavaScript。如果我嘗試運行你發佈的代碼,它會拋出一個錯誤。 –
你在期待什麼,你在接受什麼? –
我希望發現的數組能夠輸出正在學習數學或英語的學生,並花費了150分鐘的時間在他們身上,此刻我的代碼我得到了正確的學生數學和英語,但總數是完全錯誤的。 – Useer