Arrays and objects
Model real data and transform it with map, filter and reduce.
What you will be able to do
- Create and access arrays and objects
- Transform data immutably
- Destructure values
Arrays hold ordered lists. Objects hold named properties. Most application logic is transforming one shape into another.
const lessons = [
{ title: "Variables", done: true, minutes: 10 },
{ title: "Functions", done: true, minutes: 12 },
{ title: "Arrays", done: false, minutes: 14 },
];
const finished = lessons.filter((lesson) => lesson.done);
const titles = lessons.map((lesson) => lesson.title);
const totalMinutes = lessons.reduce((sum, lesson) => sum + lesson.minutes, 0);
const { title } = lessons[2];
console.log(titles, totalMinutes, title);| Method | Use it to |
|---|---|
| map | Transform every item into a new array. |
| filter | Keep only items that match a condition. |
| reduce | Fold an array into a single value. |
| find | Get the first matching item. |
| some / every | Test whether any or all items match. |
Your turn
MediumCalculate total minutes
Given the lessons array, use reduce to calculate total minutes across all lessons.
JAVASCRIPT · starter
const lessons = [
{ minutes: 10 },
{ minutes: 12 },
{ minutes: 14 },
];
// your code hereCheck your understanding before moving on.
Knowledge checkQuestion 1
Which method creates a new array by transforming each item?
Select an answer to continue
Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.