loading...

re: How to use the `reduce` Method in JavaScript (and React) VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Thank you for your feedback. Quoting MDN, the documentation does say it returns a single value. The reduce() method executes a reducer function ...
 

Single value doesn't mean it can't be a collection type I think is the OP's point. For example you can use reduce to easily make a lookup map out of an array:

[
  { id: 123, name: "Ben" },
  { id: 456, name: "Sarah" },
  { id: 789, name: "Jane" }
].reduce((acc, e) => {
  return {
    [e.id]: e.name,
    ...acc
  }
}, {})

/*
output-
{
  123: Ben,
  456: Sarah,
  789: Jane
}
*/

Yes, thank you for a nice example.

Code of Conduct Report abuse