Software Engineering :: Programming :: Languages :: JavaScript :: Array :: Method :: Instance :: Array.prototype.reduce :: Spike
⪼ Made with 💜 by Polyglot.
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);