Contents Lesson 7 of 21

8 min read · foundations

Objects and arrays — the shape real data arrives in

By the end of this lesson you will have bars.js, holding the two-bar array and four lines you wrote by hand: the closes, the bars above 310, one bar found by its date, and one search that finds nothing.

Almost every response in this track is an object, a list of objects, or an object containing a list. Learn these two and market data stops being alien.

An object is labelled values

const quote = { code: "AAPL.US", close: 309.90, volume: 25869800 };

quote.close;        // 309.90
quote["close"];     // the same
quote.missing;      // undefined — no error

That last line matters enormously. Asking an object for something it does not have gives you undefined rather than a complaint. Silence, again — and it is why a typo in a field name shows up as an empty cell rather than a crash.

An array is ordered values

const bars = [
  { date: "2026-08-24", close: 310.34 },
  { date: "2026-08-25", close: 309.90 },
];

bars.length;    // 2
bars[0].close;  // 310.34 — counting starts at zero
bars[9];        // undefined, again no error

Counting starts at zero, so the last item is at length - 1. Off-by-one at the first and last element is the most common bug in this whole track, and Backtest Strategies is largely about one specific version of it.

Three list operations do most of the work

bars.map(b => b.close)            // one output per input
bars.filter(b => b.close > 310)   // keep some
bars.find(b => b.date === "2026-08-25")  // first match, or undefined

Read them as sentences: give me the close of each, keep the ones above 310, find the one dated that. (=== means "is exactly equal to"; the next lesson explains why it has three equals signs.) Once those three read as English, most data-handling code does.

JSON is this, as text

{"code":"AAPL.US","close":309.90,"volume":25869800}

That is what a server actually sends: the same shape, written as a string. JSON.parse turns text into an object; JSON.stringify goes the other way. Every API in this academy speaks it, and unit 3 makes your first request.

Try it now

Take the two-bar array above and, by hand, write the code for: the closes as a list, the bars above 310, and the bar dated 2026-08-25. Then ask for the bar dated 2026-01-01 and see undefined come back rather than an error. That silence is a thing you now expect.

The prompt

Write bars.js yourself and run it. Then paste it into your chat assistant:

I wrote this by hand to practise map, filter and find. Do not rewrite
it. Read each of my lines back to me as an English sentence, and tell
me what it prints. Then tell me which of my lines can give back
undefined, and for what input.

[paste bars.js]

A good answer reads map as "the close of each", filter as "keep the ones above 310" and find as "the first one dated…", and names your find lines as the ones that can give back undefined.

Check it yourself

  1. The closes print as [ 310.34, 309.9 ]. The trailing zero is gone, and nothing is lost: 309.90 and 309.9 are the same number.
  2. The filter gives back a list holding one bar, the one dated 2026-08-24.
  3. The find for 2026-08-25 gives back that bar itself, an object rather than a list.
  4. The find for 2026-01-01 prints undefined, and the program carries on to its next line.

Break it on purpose

Ask the missing bar for its close: bars.find(b => b.date === "2026-01-01").close. This time the program stops with TypeError: Cannot read properties of undefined (reading 'close'). Finding nothing was quiet; asking nothing for a field is loud.

Compare with ours

Our bars.js
const bars = [
  { date: "2026-08-24", close: 310.34 },
  { date: "2026-08-25", close: 309.90 },
];

console.log(bars.map(b => b.close));                      // [ 310.34, 309.9 ]
console.log(bars.filter(b => b.close > 310));             // the 24 August bar, in a list
console.log(bars.find(b => b.date === "2026-08-25"));     // the 25 August bar
const missing = bars.find(b => b.date === "2026-01-01");
console.log(missing);                                      // undefined

The last two lines are the takeaway: find can give back nothing, so look at what it gave back before you use it. The next lesson's if is how code does that looking for you.

Checkpoint

git status   # only the files you expect, and no .env*
git add -A
git commit -m "map, filter and find on two bars, and a find that matches nothing"

Then answer this lesson's questions.