Arrow function with return in reduce call

Isaac
//Example 1:
var primaryColors = [
  { color: 'red' },
  { color: 'blue' },
  { color: 'yellow' },
];

var answer = primaryColors.reduce((x,y) => x.push(y.color) , []);
//x.push is not a function



//Example 2:
var ints = [1,2,3];
var sum = ints.reduce((sum, number) => sum + number, 0);
//sum = 6

As shown above, Example 1 will be throwing exception TypeError: x.push is not a function. In order to make it works, we can add a pair of curly braces and returning value as below.

var answer = primaryColors.reduce((x,y) => { x.push(y.color); return x;}, []);

Now I don't really understand the need of return. Why Example 2 can sum without using the keyword return while Example 1 needed a return keyword in order to work?

jfriend00

Why does Example 1 need a return keyword in order to work?

The problem is that x.push() returns the new length of the array, so that's what your callback to .reduce() returns. But .reduce() will take the return value from your callback and pass it as the next value for x in the next iteration of the loop. So you need to return x.

The way you're doing it, the second time your callback is called, x will be 1 and 1.push() is an error.

If you don't understand why you need return x, then study the doc for .reduce() more thoroughly. x is the accumulator and the value of x in the next iteration of your callback will be whatever you return from the prior one.

Why Example 2 can sum without using the keyword return

Because your example 2, returns sum + number which is what you want passed as sum for the next iteration of the loop. So, it's returning the right thing.


FYI, your first loop is perhaps more easily done with .map():

//Example 1:
var primaryColors = [
  { color: 'red' },
  { color: 'blue' },
  { color: 'yellow' },
];

var answer = primaryColors.map(item => item.color);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Return object from arrow function

From Dev

Return object from arrow function

From Dev

Using the reduce function to return an array

From Dev

Can we call new on arrow function?

From Dev

How to call a reduce function in another function

From Java

Refactor: Expected to return a value at the end of arrow function

From Java

Specify return type in TypeScript arrow function

From Dev

Expected to return a value at the end of arrow function with if statement

From Dev

Is it good to return a function call?

From Dev

Can arrow functions incorporate the multiple arguments of a host function such as reduce?

From Dev

Possible to return multiple results with reduce function?

From Dev

Return first element while using reduce function

From Dev

Function.prototype.call does not set this at Arrow Function

From Dev

Tell gcc that a function call will not return

From Dev

Return array to parent function call

From Dev

Function call with a union return type

From Dev

How to call a Function with a RETURN NUMBER

From Dev

Set a value or call a function on return

From Dev

Why does arrow assignment in R not work in a transform function call?

From Java

How to return anonymous object from one liner arrow function in javascript?

From Dev

using arrow function into the return block of page.evaluate of puppeter script

From Dev

How do you return an object with a fat arrow function without rebinding this?

From Dev

Fixing 'consistent-return' linter issue with simple arrow function

From Dev

using arrow function into the return block of page.evaluate of puppeter script

From Dev

can nested function or method call reduce performance of application

From Dev

Why does my reduce based average function return NaN?

From Dev

How can I define the return type of a lodash reduce function with Typescript?

From Dev

How to return an JSON object I made in a reduce function

From Dev

Common Lisp function for "Reduce and Return Intermediate Results as Sequence"

Related Related

  1. 1

    Return object from arrow function

  2. 2

    Return object from arrow function

  3. 3

    Using the reduce function to return an array

  4. 4

    Can we call new on arrow function?

  5. 5

    How to call a reduce function in another function

  6. 6

    Refactor: Expected to return a value at the end of arrow function

  7. 7

    Specify return type in TypeScript arrow function

  8. 8

    Expected to return a value at the end of arrow function with if statement

  9. 9

    Is it good to return a function call?

  10. 10

    Can arrow functions incorporate the multiple arguments of a host function such as reduce?

  11. 11

    Possible to return multiple results with reduce function?

  12. 12

    Return first element while using reduce function

  13. 13

    Function.prototype.call does not set this at Arrow Function

  14. 14

    Tell gcc that a function call will not return

  15. 15

    Return array to parent function call

  16. 16

    Function call with a union return type

  17. 17

    How to call a Function with a RETURN NUMBER

  18. 18

    Set a value or call a function on return

  19. 19

    Why does arrow assignment in R not work in a transform function call?

  20. 20

    How to return anonymous object from one liner arrow function in javascript?

  21. 21

    using arrow function into the return block of page.evaluate of puppeter script

  22. 22

    How do you return an object with a fat arrow function without rebinding this?

  23. 23

    Fixing 'consistent-return' linter issue with simple arrow function

  24. 24

    using arrow function into the return block of page.evaluate of puppeter script

  25. 25

    can nested function or method call reduce performance of application

  26. 26

    Why does my reduce based average function return NaN?

  27. 27

    How can I define the return type of a lodash reduce function with Typescript?

  28. 28

    How to return an JSON object I made in a reduce function

  29. 29

    Common Lisp function for "Reduce and Return Intermediate Results as Sequence"

HotTag

Archive