Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce returning undefined?

Tags:

javascript

I have an object called student, with two properties, name and score. I am trying to perform calculations using the score property but am having trouble accessing the property from the array of students. Currently, I'm trying to get the sum of the scores with the following code:

var sum = students.reduce(function(a, b) { 
                    return {sum: a.score + b.score}
                    })

This returns an undefined value and causes display weirdness in firefox. I can't seem to find the error.

Is there no way to just access parameters simply, (i.e. var myVar = myArray. myObject.myProperty;)?

like image 361
Space Ostrich Avatar asked Mar 14 '16 02:03

Space Ostrich


People also ask

What does reduce() do in js?

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.

Why is return undefined?

A variable that has not been assigned a value is of type undefined . A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned .

How reduce function works?

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.

How to use reduce on an array?

Introduction to the JavaScript Array reduce() method The script is simple and straightforward: First, declare an array of three numbers 1, 2 and 3. Second, declare the sum variable and set its value to zero. Third, in the for loop, add up the elements of the numbers array to the sum variable.


1 Answers

I think you have a misunderstanding about how reduce works. For each element in the array, it executes the provided function. The first argument is an "accumulator"; something that's passed along as each element is visited (actually, it's the return value of the function from the last element, but it's usually used as an accumulator). The second argument is the element being visited. So what I think you want is this:

var sum = students.reduce(function(a, s) {
    a.sum += s.score;
    return a;
}, { sum: 0 });

When you invoke reduce, you can provide the initial value of the accumulator (otherwise it takes on the value of the first element, and the visitation starts with the second element). Here, we provide an object with a sum property set to zero.

If all you want is the sum (not an object with a sum property), it can be even simpler:

var sum = students.reduce(function(a, s) {
    return a += s.score;
}, 0);
like image 155
Ethan Brown Avatar answered Nov 03 '22 02:11

Ethan Brown