Arrays
map, filter, reduce, find, spread
Skills taught: js.arrays, js.array-methods, js.destructuring
Creating and accessing arrays
const fruits = ['apple', 'banana', 'cherry'];
// Index access (starts at 0)
console.log(fruits[0]); // 'apple'
console.log(fruits[2]); // 'cherry'
console.log(fruits.at(-1)); // 'cherry' — last item (modern)
console.log(fruits.length); // 3
// Modify
fruits[1] = 'blueberry'; // replace
fruits.push('grape'); // add to end
fruits.pop(); // remove from end — returns 'grape'
fruits.unshift('avocado'); // add to start
fruits.shift(); // remove from start — returns 'avocado'
map — transform every item
const prices = [10, 25, 50];
// Apply 20% discount to every price
const discounted = prices.map(price => price * 0.8);
console.log(discounted); // [8, 20, 40]
// Transform array of objects
const users = [{name: 'Ana', age: 28}, {name: 'Ben', age: 34}];
const names = users.map(user => user.name);
console.log(names); // ['Ana', 'Ben']
filter — keep matching items
const scores = [45, 78, 92, 55, 88, 31];
// Keep only passing scores (>= 60)
const passing = scores.filter(score => score >= 60);
console.log(passing); // [78, 92, 88]
// Filter objects
const products = [
{name: 'Laptop', inStock: true},
{name: 'Phone', inStock: false},
{name: 'Tablet', inStock: true},
];
const available = products.filter(p => p.inStock);
console.log(available.map(p => p.name)); // ['Laptop', 'Tablet']
reduce — fold into a single value
const numbers = [1, 2, 3, 4, 5];
// Sum all numbers
const sum = numbers.reduce((total, n) => total + n, 0);
console.log(sum); // 15
// Count occurrences
const tags = ['css', 'html', 'css', 'js', 'html', 'css'];
const counts = tags.reduce((acc, tag) => {
acc[tag] = (acc[tag] ?? 0) + 1;
return acc;
}, {});
console.log(counts); // { css: 3, html: 2, js: 1 }
find, findIndex, includes, some, every
const users = [{id: 1, name: 'Ana'}, {id: 2, name: 'Ben'}, {id: 3, name: 'Cal'}];
users.find(u => u.id === 2); // {id: 2, name: 'Ben'}
users.findIndex(u => u.id === 2); // 1
users.some(u => u.name === 'Ben'); // true — at least one matches
users.every(u => u.id > 0); // true — all match
[1,2,3].includes(2); // true
Spread and destructuring
// Spread — expand array into individual values
const a = [1, 2, 3];
const b = [4, 5, 6];
const combined = [...a, ...b]; // [1, 2, 3, 4, 5, 6]
const copy = [...a]; // shallow copy
// Destructuring — unpack values into variables
const [first, second, ...rest] = [10, 20, 30, 40, 50];
console.log(first); // 10
console.log(second); // 20
console.log(rest); // [30, 40, 50]
Frequently Asked Questions
Does map/filter/reduce change the original array?
No. map, filter, and reduce return new arrays without modifying the original. sort and splice DO mutate the original. To sort without mutation: [...array].sort(...).
What is the difference between map and forEach?
map returns a new array with transformed values. forEach returns undefined — it is used for side effects like logging or DOM updates. If you need the results, use map. If you only need the side effect, forEach is fine.