Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript array reduce start from index

This problem has been bugging me for a while now and I can't seem to find an answer in web.

Is it possible to use Array reduce method starting from a certain index?

simple example

var studentGrades = ["John Doe", "Some School", 6, 7, 8, 7, 9, 9];

If I need to loop over only integers in studentGrades, I can do that with a simple for loop

for(var i = 2; i < studentGrades.length; i++) {
  // do stuff here ...
}

But let's say I would need get an average grade which is sum of all integers divided by integers count. If Array contained only integers, then there would be no problem using reduce.

var onlyIntegersArr = [5,2,3,4];
var averageGrade = onlyIntegersArr.reduce(function(a,b){
  return a + b;
}) / onlyIntegersArr.length;

However if I know that for whatever reasons I need to skip the first two Array elements and start from index Array[2].

So for example I would apply reduce to studentGrades, but only starting from index studentGrades[2].

Is that possible with reduce?

Thank you for solutions, I like slice approach, but I prefer not using a new method in this case.

e.g.

var average = studentGrades.reduce(function(a,b,i){
  return i >= 2 ? a+b : 0;
}) / (studentGrades.length - 2);
like image 961
JanisSt Avatar asked Jan 27 '16 09:01

JanisSt


People also ask

What is reduce () in JavaScript?

The reduce() method executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

Does reduce return a new array?

JavaScript Array reduce() The reduce() method executes a reducer function for array element. The reduce() method returns a single value: the function's accumulated result. The reduce() method does not execute the function for empty array elements. The reduce() method does not change the original array.

Can I use reduce on an array of objects?

If you use array reduce on an array without any elements and don't provide initialValue, it will throw a TypeError. If the array is empty and initialValue is provided, or the array has only one element and initialValue, the reduce method will return the same value without calling the callbackfn.

Is reduce faster than for loop?

Both for and for..of are 3.5 times faster than reduce .


1 Answers

reduce's 3rd argument is an index, here is the fiddle

var averageGrade = onlyIntegersArr.reduce(function (a, b, c) {
    if (c >= 2) {
        return a + b;
    } else {
        return 0;
    }
});

if array has more non-numeric items after second index then check this fiddle

var studentGrades = ["John Doe", "Some School", 6, 7, 8, 7, 9, 9, "Some School"];
var averageGrade = studentGrades.reduce(function (a, b, c) {
    if (c >= 2 && !isNaN(b)) {
        return a + b;
    } else if (c >= 2) {
        return a + 0;
    } else {
        return 0;
    }
})
alert(averageGrade);
like image 97
gurvinder372 Avatar answered Sep 21 '22 08:09

gurvinder372