JavaScript array reduces start from index

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++) {
  // do stuff here ...
}

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);
+4
4

reduce - , fiddle

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

, 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);
+3

, n , Array#slice

ES2015

var sum = array.slice(n).reduce((a, b) => a + b);

var studentGrades = ["John Doe", "Some School", 6, 7, 8, 7, 9, 9];
var sum = studentGrades.slice(2).reduce((a, b) => a + b);

document.body.innerHTML = 'SUM is = ' + sum;

ES5 .

var sum = array.slice(n).reduce(function(a, b) {
    return a + b;
});

var studentGrades = ["John Doe", "Some School", 6, 7, 8, 7, 9, 9];
var sum = studentGrades.slice(2).reduce(function(a, b) {
    return a + b;
});

document.body.innerHTML = 'SUM is = ' + sum;

, , , : -

var sum = array.reduce(function(result, v) {
    return result + (parseFloat(v) || 0);
}, 0);

var studentGrades = ["John Doe", "Some School", 6, 7, 8, 7, 9, 9];
var sum = studentGrades.reduce(function(result, v) {
    return result + (parseFloat(v) || 0);
}, 0);

document.body.innerHTML = 'SUM is = ' + sum;
+6

If you are sure that you always need only index 2, then this is enough

var onlyIntegersArr = studentGrades.slice(2);
var averageGrade = onlyIntegersArr.reduce(function(a,b){
    return a + b;
}) / onlyIntegersArr.length;

If, however, you want to get all integers, you need to filter the array

var onlyIntegersArr = studentGrades.filter(function(val) {
    return (val === parseInt(val, 10));
});
var averageGrade = onlyIntegersArr.reduce(function(a,b){
    return a + b;
}) / onlyIntegersArr.length;
0
source

Why are you compromising too much? Why not:

function avg(arr)
{
     var sum = 0;
     var l = 0;
     for(var i = 0; i < arr.length; i++)
     {
        if(isNaN(1*arr[i])) continue;
        sum += arr[i];
        l++;
     }
     return sum/l;
}

Perhaps you need to think about storing data in an object where all estimates are in a separate array. And other data will be in the properties. You can serialize it from the array you have, and then work with the object.

0
source

Source: https://habr.com/ru/post/1626144/


All Articles