Lists of Objects

Data arrives as a list of records

One object holds one record. Real data almost never comes as a single record, it comes as a whole stack of them: every booth at a market, every rider in a race, every reading a sensor took overnight.
An array of objects is how that stack is written down. The array keeps the records in order, and each element inside it is a complete record carrying its own fields.

Two steps to reach one field

booths[1] is an element of the array, and that element is a whole record. Adding .sales takes one more step inward, to a single field of that one record.
Read it left to right: pick the record, then pick the field.

One pass, one record

A for-of loop over an array of objects hands the body one whole record per pass. The loop variable IS that record, so every field of it is one dot away.

Totalling and counting one field

Both jobs use the same skeleton: a variable set up before the loop, one record examined per pass, and the variable printed after the loop has finished. Only the middle changes.

The operator decides who sits on the line

A record whose field lands exactly on the number in the test is the one to watch. Written with >=, that record passes and is counted. Written with >, the very same record falls through and is left out. Nothing else about the loop changes, so read the operator before you start counting.
The choice matters again when two records tie for the largest. Written with >, a record that merely matches the leader is not an improvement, so the leader stays put. Written with >=, matching is enough to take over. Trace the passes and you will see which of the tied records ends up being held.

Keep the record, not just the number

To find the largest, hold on to the best record seen so far and compare its field against the next one. Assigning the whole record is the part that matters. At the end you still have its name, the field you compared, and everything else it was carrying.
The program below prints each team's record, totals the wins, counts the teams at or above a target, and keeps the record with the most wins. Run it, then make each change below and run it again. Press Reset before each change.
• Change >= to > in the line if (t.wins >= target) {.
• Add a sixth team, { name: "Wolves", wins: 17 }, below the Lynx.
• Change the Bears' 11 to 14 and run it. Then change t.wins > leader.wins to t.wins >= leader.wins and run it again.
let teams = [
  { name: "Hawks", wins: 9 },
  { name: "Owls", wins: 14 },
  { name: "Foxes", wins: 5 },
  { name: "Bears", wins: 11 },
  { name: "Lynx", wins: 12 },
];
let target = 9;
let total = 0;
let atTarget = 0;
let leader = teams[0];
for (const t of teams) {
  console.log(t.name + " won " + t.wins);
  total = total + t.wins;
  if (t.wins >= target) {
    atTarget = atTarget + 1;
  }
  if (t.wins > leader.wins) {
    leader = t;
  }
}
console.log("Total wins: " + total);
console.log("Teams with " + target + " or more wins: " + atTarget);
console.log("Most wins: " + leader.name + " " + leader.wins);
Output
Press Run to see what this program prints.

Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.

Unpacking the fields in the header

When the body of a loop reaches for the same two or three fields over and over, the header can name them directly and save the dots. This is called destructuring.
ExpressionWhat it gives you
boothsthe whole array of records
booths.lengthhow many records the array holds
booths[2]one record, complete with its fields
booths[2].nameone field of one record
b.sales, inside for (const b of booths)that field of the record this pass is holding