Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Last active December 11, 2024 17:18
Show Gist options
  • Select an option

  • Save wilmoore/2045113039ed8f29ec09001c8b54b969 to your computer and use it in GitHub Desktop.

Select an option

Save wilmoore/2045113039ed8f29ec09001c8b54b969 to your computer and use it in GitHub Desktop.
Software Engineering :: Programming :: Languages :: JavaScript :: Array :: Method :: Instance :: Array.prototype.reduce :: Spike

Software Engineering :: Programming :: Languages :: JavaScript :: Array :: Method :: Instance :: Array.prototype.reduce :: Spike

⪼ Made with 💜 by Polyglot.

related

Array.prototype.reduce Spike

const cells = [
  { pos: "B2", val: 100 },
  { pos: "B3", val: 200 },
  { pos: "B4", val: 300 },
];

// create a new list including only items where the cell value is above `150`
const filtered = cells.filter((cell, idx, cells) => cell.val > 150);

// transform list of cells to list of cell position strings
const range = cells.map((cell) => cell.pos);

// list of filtered cell positions
const filtered_range = filtered.map((cell) => cell.pos);

// reduce to the `sum` (`number`) of all cell values `number`
const sum = cells.reduce((returnValue, cell) => returnValue + cell.val, 0);

// reduce to the `sum` (`number`) of all cell values
const filtered_sum = filtered.reduce((returnValue, cell) => returnValue + cell.val, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment