Just like arrays, structs have functional-style operations that take a closure instead of requiring a manual for...in loop. The main difference: every struct closure receives the key first, then the value — the opposite order you might expect coming from arrays, where the value comes first.
Learning Objectives
After completing this lesson, you'll be able to:
- Run a function for every key/value pair with structEach.
- Transform every value with structMap.
- Keep only matching entries with structFilter.
- Reduce a struct to a single value with structReduce.
- Check whether all, or any, entries match a condition with structEvery and structSome.
structEach — Run Something for Every Entry
Like arrayEach, this doesn't return anything — it's for side effects, like output or logging.
structMap — Transform Every Value
Returns a new struct with the same keys, where each value is whatever the callback returns. The original struct isn't changed.
structFilter — Keep Only Matching Entries
structReduce — Combine Into a Single Value
Like arrayReduce, this carries an accumulated result forward through every entry. The callback receives the running result first, then the key, then the value.
As with arrayReduce, always pass an explicit starting value (0 here) as the third argument.
structEvery and structSome
Real-World Example: Applying a Discount
Common Beginner Mistakes
Assuming the callback receives (value, key) like array functions do
Struct closures receive (key, value), not (value, key) — the reverse of the array versions. Mixing this up silently reads the wrong data in each parameter.
Forgetting structMap and structFilter return new structs
The original struct isn't modified — you need to capture the return value in a new variable, the same as with the array versions.
Forgetting structReduce's starting value
Without the third argument, the reduce doesn't start from a clean value like 0 — always pass one explicitly for predictable results.
Best Practices
- Remember the (key, value) order specifically for struct closures — it's easy to mix up with the array equivalents.
- Use structFilter + structMap together, the same chaining pattern as arrays, to narrow down and transform in two clear steps.
- Use structEach only for side effects — use structMap when you need the transformed data back.
Interview Questions
What order does a struct closure's parameters come in?
(key, value) — the opposite of array closures, which receive (value, index).
What does structFilter's callback need to return?
A Boolean — true to keep that entry in the result, false to exclude it.
What's the difference between structEach and structMap?
structEach runs a function for every entry but returns nothing. structMap returns a new struct with each value replaced by the callback's return value.
Summary
In this lesson, you learned struct's functional operations: structEach for side effects, structMap to transform values, structFilter to keep matching entries, structReduce to combine into one value, and structEvery / structSome for all-or-any checks — all using the (key, value) callback order.
What's Next?
The next lesson covers sorting and searching structs — structSort, structKeyArray, and the recursive structFindKey / structFindValue.