DMarlon Studio

Lesson 14 · 14 of 20

reduce, find, some and every · JavaScript

Summarise and search JavaScript arrays: reduce to add up or group, find and findIndex to locate, some and every to check.

JavaScript · reduce, find, some and every in DMarlon Studio: the example's code and its output in the terminal

`reduce()` combines all the elements into a single value. It takes a function with the accumulator (sum) and the current element (o), plus a starting value, 0. On each round, whatever the function returns becomes the new accumulator: at the end, sum is the total of all the orders.

The starting value can be anything. With an empty object {}, reduce groups the amounts by customer: acc[o.customer] || 0 takes what that customer already had, or 0 if this is the first one, and adds the order.

Without a starting value, reduce uses the first element as the accumulator. That is how it keeps the biggest order, comparing each one with the best so far.

`find()` returns the first element that passes the test, or undefined if none does, as with order 999. `findIndex()` returns its position instead of the element.

`some()` tells you whether at least one element passes and `every()` whether they all do. Both return true or false and stop looking as soon as they know the answer.

Code · main.js

const orders = [  { id: 101, customer: "Ana", total: 35.5, paid: true },  { id: 102, customer: "Luis", total: 120, paid: true },  { id: 103, customer: "Marta", total: 64.25, paid: false },  { id: 104, customer: "Ana", total: 18, paid: true },];// reduce combines all the elements into one value: here, a sum.const revenue = orders.reduce((sum, o) => sum + o.total, 0);console.log("Total of all orders:", revenue);// The starting value can be anything, such as an object to group.const perCustomer = orders.reduce((acc, o) => {  acc[o.customer] = (acc[o.customer] || 0) + o.total;  return acc;}, {});console.log("Per customer:", perCustomer);// Without a starting value, reduce begins with the first element.const biggest = orders.reduce((best, o) => (o.total > best.total ? o : best));console.log("Biggest order:", biggest.id, "by", biggest.customer);// find returns the FIRST element that matches (or undefined).const unpaid = orders.find((o) => !o.paid);console.log("First unpaid:", unpaid.id, unpaid.customer);console.log("Order 999:", orders.find((o) => o.id === 999));console.log("Position of 103:", orders.findIndex((o) => o.id === 103));// some: is there at least one? every: are all of them?console.log("Any order over 100?", orders.some((o) => o.total > 100));console.log("All paid?", orders.every((o) => o.paid));

Output · node main.js

Total of all orders: 237.75
Per customer: { Ana: 53.5, Luis: 120, Marta: 64.25 }
Biggest order: 102 by Luis
First unpaid: 103 Marta
Order 999: undefined
Position of 103: 2
Any order over 100? true
All paid? false

Key points

  • reduce((acc, x) => ..., start) boils an array down to one value.
  • With {} as the start, reduce can group or count.
  • find gives the first matching element, or undefined.
  • some: any of them? every: all of them?

Exercise

Use reduce to count how many orders are paid and unpaid, find to get Ana's first order over 20 euros, and every to check whether all of them are over 10 euros.