2016-12-09 102 views
2

我有這個對象數組,我想計算學生根據某些條件花費在特定主題上的時間。如何找到一個數組中的對象的總和

我有這段代碼來識別主題: 但我很努力地找到這些特定主題的總分鐘。

$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 
      } 
     ] 
    } 
] 
+0

你沒有張貼有效的JavaScript。如果我嘗試運行你發佈的代碼,它會拋出一個錯誤。 –

+1

你在期待什麼,你在接受什麼? –

+0

我希望發現的數組能夠輸出正在學習數學或英語的學生,並花費了150分鐘的時間在他們身上,此刻我的代碼我得到了正確的學生數學和英語,但總數是完全錯誤的。 – Useer

回答

1

你可以這樣做:

var 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}]}], 
 
    getTotalMinutes = function (time) { 
 
     return time 
 
     .map(function (s) { 
 
      return s.minutes; 
 
     }) 
 
     .reduce(function (a, b) { 
 
      return a + b; 
 
     }, 0); 
 
    }, 
 
    result = students.map(function (student) { 
 
     return { 
 
     [student.name]: getTotalMinutes(student.time) 
 
     }; 
 
    }); 
 

 
console.log(result);

+0

@Useer問題是要花費總時間的總和? –

+0

非常感謝你,它看起來也很棒。 – Useer

2

這是使用Javscript reducefilter功能一個很好的候選人。

過濾器將針對數組中的每個條目運行一個函數,僅返回該函數返回的值爲true的值。

Reduce將在迭代數組中的每個條目時保持總值。你可以說它習慣於將一個數組「減少」成一個單一的值,這就是我們在這裏要做的。

下面是一個例子,考慮到students變量包含學生的陣列:

// We'll match against this variable 
 
var desiredSubject = "maths" 
 

 
// First, we iterate over each student in the array 
 
var time = students.reduce(function(previousTime, student) { 
 
    // Inside the array is another array, so we want to 
 
    // reduce that one after filtering it to match our subject 
 
    var subjectTime = student.time.filter(function(subject) { 
 
    // Keep only subjects that match ours 
 
    return subject.subject == desiredSubject; 
 
    }).reduce(function(previousMinutes, subject) { 
 
    // Then sum up the minutes from each one 
 
    return previousMinutes + subject.minutes; 
 
    }, 0); // Our initial sum is 0 
 
    // Sum up each student's minutes 
 
    return previousTime + subjectTime; 
 
}, 0); // Again, with an initial sum of 0

請記住,一個共同的「疑難雜症」與reduce功能是,你有你起始值之後的減少功能。忘記放在那裏很容易。

+0

非常感謝你,它看起來很棒,一定會引導我朝着正確的方向發展:-) – Useer

+0

我會在Angular中添加一個這樣的東西,如果只是將它放在「服務」上是一個好習慣如果要顯示給用戶,可以在代碼或「過濾器」中使用。這樣可以減少範圍內的代碼量(如果是過濾器),如果'scope.students'數組發生更改,視圖將自動更新。 –

1

您可以使用students.reduce產生的學科集合隨着時代的每個總和:

var 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 
 
    }] 
 
}] 
 

 
var subjectTimes = students.reduce(function(result, student) { 
 
    student.time.reduce(function(prev, time) { 
 
     if (time.subject in result) 
 
      result[time.subject] += time.minutes; 
 
     else 
 
      result[time.subject] = time.minutes; 
 
    }, {}) 
 
    return result; 
 
}, {}); 
 

 
console.log(subjectTimes);

相關問題