Playwright + TS · Lesson 3 of 36

0/36 done

Module 3: Arrays & Objects

3. Arrays, Objects & Functional Methods (map, filter)

14 minlesson

Your task

Given an array of test cases:

  1. Filter the array to get only test cases with status === "failed".
  2. Map the filtered test cases to extract only their name strings.

Write it in the editor, then press Check answer (Ctrl + Enter).

Essential Array Methods in Automation

typescript
const products = [
  { name: "Pro Laptop", price: 1200, inStock: true },
  { name: "USB Cable", price: 15, inStock: false },
  { name: "Wireless Mouse", price: 45, inStock: true },
];

// 1. map(): transform each item into another shape
const productNames = products.map((p) => p.name);
// Result: ["Pro Laptop", "USB Cable", "Wireless Mouse"]

// 2. filter(): extract items matching a criteria
const availableProducts = products.filter((p) => p.inStock);

// 3. find(): return the first matching item or undefined
const cable = products.find((p) => p.name === "USB Cable");

// 4. some() & every(): check boolean conditions
const hasExpensiveItem = products.some((p) => p.price > 1000); // true
const allInStock = products.every((p) => p.inStock);           // false

Working with JSON

  • JSON.stringify(data): Converts JavaScript objects into a JSON string for API payloads.
  • JSON.parse(jsonString): Converts raw JSON strings back into JavaScript objects.

Key Takeaways & Walkthrough Summary

In test automation, you frequently work with lists of web elements, test data tables, and API responses. In this lesson, we will master arrays and objects, along with powerful methods like map, filter, find, and reduce. We will also learn how to parse and stringify JSON data. Let's get hands-on!

Part of the Playwright + TS course (Playwright with TypeScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

What is the difference between find() and filter()?

find() stops at the first matching element and returns a single item (or undefined). filter() iterates the entire array and returns an array of all matching items.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

TypeScript
Skip