Playwright + JS · Lesson 3 of 35

0/35 done

Module 3: Arrays & Objects

3. Arrays, Objects and JSON for Test Data

15 minlesson

Try it yourself

Implement filterActiveUsernames:

  1. Accept users array of objects containing { name, active }.
  2. Filter for active === true, then map to return array of user names.

The editor has a worked example. Read it, tweak it, then press Mark as complete.

Higher-Order Array Methods for Automation

javascript
const testUsers = [
  { id: 1, name: "Admin", role: "ADMIN", active: true },
  { id: 2, name: "Tester", role: "QA", active: true },
  { id: 3, name: "Guest", role: "VIEWER", active: false },
];

// Extract usernames with map:
const usernames = testUsers.map(u => u.name);

// Filter active users:
const activeUsers = testUsers.filter(u => u.active);

// Check if any admin exists:
const hasAdmin = testUsers.some(u => u.role === "ADMIN");

// JSON serialization:
const jsonString = JSON.stringify(testUsers);
const parsedData = JSON.parse(jsonString);

Key Takeaways & Walkthrough Summary

In automation, you will constantly parse test data files, filter table records, and validate API JSON responses. In this lesson, we will master arrays, objects, higher-order methods like map and filter, and JSON serialization. Let's get hands-on!

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

Common Doubts & Lesson Q&A

When should I use find() vs filter()?

find() returns the single first matching item (or undefined). filter() returns a new array with all matching items.

Finished reading?

Review the worked example in the editor, then mark the lesson complete.

JavaScript
Skip