0

In Mozilla's Reduce docs page I found the syntax below:

arr.reduce(callback[, initialValue])

The square brackets throw me off.

I have code like this that works:

const selectedItems = selectedItemsList.reduce((itemObject, item) => {
  return {
    ...itemObject,
    [item]: this.props.areasList.find(area => area.id === item)
  };
}, {});

I do not use square brackets. I provide a callback and an initial value, separated by a comma. Can anyone elaborate on why the doc shows the syntax that it does for reduce? I only figured out the code in this post that I have by looking at other examples.

smuggledPancakes
  • 9,881
  • 20
  • 74
  • 113

1 Answers1

1

The square brackets mean that the initialValue parameter is optional, i.e. you don't have to provide it. You shouldn't actually place these brackets in your code.

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177