Arrays and objects
Store collections and transform them with map, filter and reduce.
const fruits = ["apple", "pear"];
fruits.push("plum");
console.log(fruits[0], fruits.length); // apple 3
const user = { name: "Ana", age: 31 };
console.log(user.name, user["age"]);
user.country = "ES";map, filter, reduce
const nums = [1, 2, 3, 4];
const doubled = nums.map((n) => n * 2); // [2, 4, 6, 8]
const evens = nums.filter((n) => n % 2 === 0); // [2, 4]
const sum = nums.reduce((a, n) => a + n, 0); // 10These methods return new arrays and leave the original unchanged, which keeps code easier to follow.
Destructuring and spread
const { name, age } = user;
const more = [...fruits, "kiwi"];
const copy = { ...user, age: 32 };JSON
const text = JSON.stringify(user); // object to string
const back = JSON.parse(text); // string to objectNoteJSON is the common format for sending data between servers and browsers. Keys and strings must use double quotes.
Try it
Test yourself
Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.