JavaScript — Arrays & Objects
Learn to work with collections of data — arrays for lists and objects for structured information.
What you'll learn
- Create and manipulate arrays (lists of items)
- Create and access object properties
- Use common array methods (push, filter, map, find)
- Understand destructuring basics
Playwright AutomationlessonsJump to another lesson
JavaScript --- Arrays & Objects
Real programs deal with lists and structured data.
Arrays --- Ordered Lists
const fruits = ["apple", "banana", "cherry"];
// 0 1 2
fruits[0]; // "apple"
fruits[2]; // "cherry"
fruits[3]; // undefined (doesn't exist)
fruits.length; // 3
Adding & removing
const fruits = ["apple", "banana"];
fruits.push("cherry"); // ["apple", "banana", "cherry"]
fruits.pop(); // removes & returns "cherry"
fruits.includes("banana"); // true
Array Methods You’ll Use A LOT
transform every item by the rule
[1, 2, 3].map(n => n * 2); // [2, 4, 6]
["ab", "cd"].map(s => s.toUpperCase()); // ["AB", "CD"]
keep items where rule is true
[1, 2, 3, 4, 5, 6].filter(n => n % 2 === 0); // [2, 4, 6]
[45, 82, 90].filter(score => score >= 70); // [82, 90]
return the FIRST item matching the rule
["apple", "banana"].find(f => f === "banana"); // "banana"
["apple", "banana"].find(f => f === "grape"); // undefined
["apple", "banana"].forEach(f => console.log(`I like ${f}!`));
Quick check
You have [1, 2, 3, 4]. You want to keep only numbers greater than 2. Which method do you reach for?
Objects --- Labeled Containers
user
3 fieldsconst user = {
name: "Alice",
age: 25,
isAdmin: false
};
user.name; // "Alice" — dot notation
user["age"]; // 25 — bracket notation
user.email = "a@b.com"; // add a new property
user.age = 26; // change an existing one
Nested objects
const config = {
browser: { name: "chrome", headless: true },
timeout: 30000
};
config.browser.name; // "chrome" — chain dots
Destructuring --- A Handy Shortcut
Long way
- const name = user.name;
- const age = user.age;
- Repetitive once you have 3+ properties
With destructuring
- const { name, age } = user;
- One line, same result
- Variable names must match property names
const user = { name: "Alice", age: 25 };
const { name, age } = user;
// name === "Alice", age === 25
// Array destructuring by position
const fruits = ["apple", "banana", "cherry"];
const [first, second] = fruits;
// first === "apple", second === "banana"
Arrays of Objects (Very Common!)
A list of things where each thing has many properties:
const tests = [
{ name: "Login", passed: true, duration: 1200 },
{ name: "Search", passed: true, duration: 800 },
{ name: "Checkout", passed: false, duration: 3500 }
];
// How many passed?
tests.filter(t => t.passed).length; // 2
// Names of failed tests
tests.filter(t => !t.passed).map(t => t.name); // ["Checkout"]
// Find a specific one
tests.find(t => t.name === "Login"); // { name: "Login", ... }
This pattern --- array of objects, queried with .filter() / .map() / .find() --- is everywhere in real code.
Quick check
To pull the 'name' property out of an object called user, use: const { } = user;
JSON --- How Data Travels
# Object → JSON text (stringify)$ JSON.stringify({ name: 'Alice', age: 25 })'{"name":"Alice","age":25}'# JSON text → Object (parse)$ JSON.parse('{"name":"Alice"}').name'Alice'Try it in your terminal!
You’ll use JSON.stringify() and JSON.parse() constantly when testing APIs.
Practice Time
Exercise 1: Create an Array and Use Filter and Find
Fill in the blanks to create an array of scores, filter out passing scores (70 or above), and find the first perfect score (100).
const scores = [85, 42, 100, 67, 91, 55]; // Keep only scores that are 70 or above const passing = scores.(score => score 70); console.log(passing); // [85, 100, 91] // Find the first score that is exactly 100 const perfect = scores.(score => score 100); console.log(perfect); // 100
Exercise 2: Match Array Methods to Their Descriptions
Match each array method to what it does: