3. Arrays, Objects and JSON for Test Data

Higher-Order Array Methods for Automation

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);

Task

  • Implement filterActiveUsernames:
  • 1. Accept users array of objects containing { name, active }.
  • 2. Filter for active === true, then map to return array of user names.
πŸ’‘Common doubts
Did you like the problem?
Lesson 3 of 35
JavaScript
Loading code editor…
Next