JavaScript Foundations·Language basics

Arrays and objects

Model real data and transform it with map, filter and reduce.

Beginner 14 min read Lesson 3 of 480 Challenge included
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.

arrays.jsJavaScript
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);
MethodUse it to
mapTransform every item into a new array.
filterKeep only items that match a condition.
reduceFold an array into a single value.
findGet the first matching item.
some / everyTest whether any or all items match.
Your turn

Calculate total minutes

Medium

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 here
Check 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 in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 3 of 480 · 0% of course