This problem has been eroding me for a long time, and I can not find the answer on the Internet.
Is it possible to use the method of reducing the array, starting with a specific index?
simple example
var studentGrades = ["John Doe", "Some School", 6, 7, 8, 7, 9, 9];
If I need to iterate over only integers in studentGrades, I can do this with a simple loop loop
for(var i = 2; i < studentGrades.length; i++) {
}
But let me say that I need to get a middle class that is the sum of all integers divided by the number of integers. If the array contained only integers, then using the reduction would not be a problem.
var onlyIntegersArr = [5,2,3,4];
var averageGrade = onlyIntegersArr.reduce(function(a,b){
return a + b;
}) / onlyIntegersArr.length;
However, if I know that for some reason I need to skip the first two Array elements and start with the index [2] array.
So, for example, I would apply the abbreviation to studentGrades, but only starting with index studentGrades [2].
Is this possible with a decrease?
, , .
.
var average = studentGrades.reduce(function(a,b,i){
return i >= 2 ? a+b : 0;
}) / (studentGrades.length - 2);