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

Essential Array Methods in Automation

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.

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.
πŸ’‘Common doubts
Did you like the problem?
Lesson 3 of 36
TypeScript
Loading code editor…
Next