0

The .reduce() method below, taken from this SO answer contains many parameters (appearantly called destructuring objects).

What functionality do these parameters represent?

let {result} = array.reduce(({arg_1, arg_2, arg_3}, arg_4)=> {
}, {result: [], arg_2: 0, arg_3: undefined})
return result
Community
  • 1
  • 1
Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232

3 Answers3

2

In case OP stumbles over the destructuring notation, for the given example, the first parameter is the initial value (described with the provided links of the first two comments), working as a collector or accumulator object. It is made up of the first three arguments of another method. The result is written by a destructuring assignment, thus, there will be 3 variables, arg_1, arg_2 and arg_3 derived from the equally named arguments of the finally returned collector object.

edit, for the OP's provided example code has changed ...

The next example takes the OP's linked SO code reference into account ...

let {slots} = array.reduce(({slots, count, prev}, item)=> {

    // ... do something with slots
    // ...
    // while iterating always needs to return
    // an equally structured collector object

    return {slots, count: count + 1, prev: item}

}, {slots: [], count: 0, prev: undefined})

/*
return slots; // does not really make sense within this shortened example's context.
*/

... is equal to ...

var slots = array.reduce(function (collector, item) {

    var
        slots = collector.slots,
        count = collector.count,
        prev  = collector.prev;

    // ... do something with slots
    // ...
    // while iterating always needs to return
    // an equally structured collector object

    return {slots: slots, count: count + 1, prev: item};

}, {slots: [], count: 0, prev: undefined}).slots;
Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
2

The reduce method accepts two arguments.

Syntax:

arr.reduce(callback[, initialValue])

So, in your example the callback is used as first argument is arrow function. Let's break it:

1. ({arg_1, arg_2, arg_3}, arg_4)=> {}  //the callback
2. {arg_1: [], arg_2: 0, arg_3: undefined} // the initial value

And ({arg_1, arg_2, arg_3}, arg_4) has two arguments for arrow function, first is declared as object.

You can notice that the initial value for arg_4 is missing.

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
1

Array.reduce takes 2 parameters,

  1. callback
  2. initialValue

Array.reduce's callback gets 4 parameters:

  1. previousValue
  2. currentValue
  3. currentIndex
  4. array

Value of previousValue is initialValue at the start of loop. If no value is passed, first element is taken as previousValue and currentValue will have next value. From them, previousValue will hold the value returned in previous iteration.

Sample

No initial value

var a = [1,2,4,4,5,6,7,8];

a.reduce(function(p,c,i,a){
  console.log(p,c,i,a);
  return c
})

With initial value

var a = [1,2,4,4,5,6,7,8];

a.reduce(function(p,c,i,a){
  console.log(p,c,i,a);
  return c
},0)

Reference

Array.reduce

Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79