JavaScript 04 · JavaScript in general
Arrays I: map, filter and friends
Indexes, map, filter, find, some, every, forEach and method chaining.
Loading lesson…
JavaScript 04 · JavaScript in general
Indexes, map, filter, find, some, every, forEach and method chaining.
Loading lesson…
An array is an ordered row of values – like the carriages of a train or crates on a conveyor belt. Each one has its position number (index), and counting starts at zero.
Picture a fruit sorting plant with a conveyor belt. You put a crate of apples on the belt, and depending on which machine stands at the end, you get something different:
map) – every apple goes through the machine and comes out peeled. As many apples come out as went in.filter) – lets only some apples through. The apples don't change, there are just fewer of them.find) – stops the belt at the first apple you're looking for and hands it to you.some/every) – only says "yes" or "no": is there at least one rotten one? Are they all ripe?All these machines work the same way: they get a function (a callback) and call it for every item. You only write what should happen to one item. In React this is your daily bread – every list on a page is made with map, every search with filter.
const fruits = ['apple', 'pear', 'plum']const empty = []const mixed = [1, 'text', true, { a: 1 }, [1, 2]] // an array can hold anythingfruits.length // 3fruits[0] // 'apple' – the first item has index 0fruits[2] // 'plum' – the last one has index length - 1fruits[3] // undefined – out of range isn't an errorfruits.at(-1) // 'plum' – a negative index counts from the endfruits.at(-2) // 'pear'fruits.length === 0 // is the array empty?Array.isArray(fruits) // true – that's how to recognise an array (typeof returns 'object')
The most common shape of data in a real app – and in React too:
const users = [{ id: 1, name: 'Anna', city: 'London' },{ id: 2, name: 'Peter', city: 'Boston' },]users[1].name // 'Peter'users[0].city // 'London'users.at(-1).id // 2
You don't always have to write an array out by hand in square brackets. Sometimes you need to make one – five rating stars, page numbers 1 to 10 or the letters of a word:
Array.from({ length: 3 }, (_, i) => i + 1) // [1, 2, 3] – an array of a given lengthArray.from('hey') // ['h', 'e', 'y']'a,b,c'.split(',') // ['a', 'b', 'c'][...'hey'] // ['h', 'e', 'y']
Array.from(x) makes an array from something you can go through piece by piece. A text is a row of characters, so 'hey' becomes three letters.Array.from({ length: 3 }, function) means "an array with three items". The object { length: 3 } only says how many items you want, and the function in the second argument makes each item. It gets two parameters: the value (there is none here, so it's called _ – an underscore is the habit for "I don't need this parameter") and the index i, that is 0, 1, 2 in turn. It returns i + 1, so you get [1, 2, 3].split(',') cuts a text wherever the separator is – handy for data like 'apple,pear,plum'.[...'hey'] does the same as Array.from('hey'): the three dots (spread) take the text apart into single characters and the square brackets put them into an array. You know spread from JavaScript 03.const fruits = ['apple', 'pear', 'plum']fruits.includes('pear') // truefruits.includes('banana') // falsefruits.indexOf('plum') // 2fruits.indexOf('banana') // -1 – "not found"
const fruits = ['apple', 'pear', 'plum']// The classic for – when you need the index or want to skip// (i starts at 0, goes up by 1 after every round, and the loop runs while i is less than the array's length)for (let i = 0; i < fruits.length; i++) {console.log(i, fruits[i])}// for…of – the most readable when you just want to go through the itemsfor (const fruit of fruits) {console.log(fruit)}// for…of with the index – entries() gives pairs [index, item] and [i, fruit] unpacks them at once// (array destructuring from JavaScript 03)for (const [i, fruit] of fruits.entries()) {console.log(i, fruit)}// forEach – calls a function for every item; the second parameter i is the index of the itemfruits.forEach((fruit, i) => console.log(i, fruit))// map – also calls a function for every item, but builds a new array from what the function returnsconst loud = fruits.map((fruit) => fruit.toUpperCase())console.log(loud) // ['APPLE', 'PEAR', 'PLUM']console.log(fruits) // ['apple', 'pear', 'plum'] – the original array stayed as it was
The first three ways just go through the array and do something with each item (here they print it). map goes through it too, but also returns a new array with the results – that's why it sits next to them in the table. The very next section covers it in detail.
| for / for…of | forEach | map | |
|---|---|---|---|
| Returns | nothing (it’s a statement) | undefined | a new array |
| Can you stop it (break = "end the loop right now")? | yes | no | no |
| When | a loop with break or with await (that comes in JavaScript 07) | a side effect for every item (log, save) | when you want to turn an array into another array |
map calls your function for every item and builds a new array of the same length from its return values. The original array doesn't change.
const numbers = [1, 2, 3, 4]numbers.map((n) => n * 2) // [2, 4, 6, 8]numbers.map((n) => n * n) // [1, 4, 9, 16]numbers.map((n) => `#${n}`) // ['#1', '#2', '#3', '#4']numbers.map((n) => n > 2) // [false, false, true, true]numbers.map(String) // ['1', '2', '3', '4'] – a ready-made function as the callbacknumbers // [1, 2, 3, 4] – unchanged
const users = [{ id: 1, first: 'Anna', last: 'Smith', age: 31 },{ id: 2, first: 'Peter', last: 'Brown', age: 17 },]// Pull out one propertyusers.map((u) => u.id) // [1, 2]// Compute something newusers.map((u) => `${u.first} ${u.last}`) // ['Anna Smith', 'Peter Brown']// Destructuring right in the parameterusers.map(({ first, age }) => `${first} (${age})`) // ['Anna (31)', 'Peter (17)']// A new object with selected properties – MIND the parenthesesusers.map((u) => ({ id: u.id, name: u.first })) // [{ id: 1, name: 'Anna' }, …]// A copy with an added property (the original objects stay as they are)users.map((u) => ({ ...u, isAdult: u.age >= 18 }))
const fruits = ['apple', 'pear', 'plum']fruits.map((fruit, i) => `${i + 1}. ${fruit}`) // ['1. apple', '2. pear', '3. plum']fruits.map((_, i) => i) // [0, 1, 2] – _ = "I don't need this parameter"
filter calls a function for every item. When the function returns true (or anything truthy), the item gets into the new array – whole and unchanged.
const numbers = [5, 12, 8, 130, 44]numbers.filter((n) => n > 10) // [12, 130, 44]numbers.filter((n) => n % 2 === 0) // [12, 8, 130, 44] – even onesnumbers.filter((n) => n > 1000) // [] – no match = an empty array, not undefined
const products = [{ id: 1, name: 'Apple', price: 12, inStock: true, category: 'fruit' },{ id: 2, name: 'Pear', price: 15, inStock: false, category: 'fruit' },{ id: 3, name: 'Carrot', price: 6, inStock: true, category: 'vegetables' },]products.filter((p) => p.inStock) // Apple, Carrotproducts.filter((p) => !p.inStock) // Pearproducts.filter((p) => p.category === 'fruit') // Apple, Pearproducts.filter((p) => p.price >= 10 && p.inStock) // Apple// Remove an item by id – "keep everything EXCEPT"products.filter((p) => p.id !== 2) // Apple, Carrot// Searchingconst query = 'ap'products.filter((p) => p.name.toLowerCase().includes(query.toLowerCase())) // Apple// Throw out empty values['a', '', null, 'b', undefined].filter(Boolean) // ['a', 'b']
The last line uses the same shortcut as map(String) above: Boolean (also from JavaScript 01) turns a value into true/false, and filter calls it for every item. An empty text, null and undefined come out false, so filter throws them out. Which values count as "false" (and what "anything truthy" at the start of this section means) is covered in JavaScript 06.
const users = [{ id: 1, name: 'Anna', city: 'London' },{ id: 2, name: 'Peter', city: 'Boston' },{ id: 3, name: 'Eve', city: 'Boston' },]users.find((u) => u.id === 2) // { id: 2, name: 'Peter', city: 'Boston' } – an object, not an arrayusers.find((u) => u.city === 'Boston') // Peter – only the FIRST matchusers.find((u) => u.id === 99) // undefinedusers.findIndex((u) => u.id === 3) // 2 – the positionusers.findIndex((u) => u.id === 99) // -1users.findLast((u) => u.city === 'Boston') // Eve – searches from the end
| find | filter | |
|---|---|---|
| Returns | one item (or undefined) | always an array (even an empty one) |
| Stops | at the first match | goes through everything |
| Typically | a detail by id | a list by a condition |
const products = [{ name: 'Apple', price: 12, inStock: true },{ name: 'Pear', price: 15, inStock: false },]products.some((p) => !p.inStock) // true – is at least one sold out?products.some((p) => p.price > 100) // falseproducts.every((p) => p.price < 20) // true – are they all cheaper than 20?products.every((p) => p.inStock) // false// Typical use (cart = an array of items in the cart, fields = an array of form fields)const canCheckout = cart.every((item) => item.inStock)const hasErrors = fields.some((f) => f.error)
Because both map and filter return an array, you can call another method on the result straight away. Read it top to bottom like the steps of a recipe:
const result = products.filter((p) => p.inStock) // 1. only in stock.filter((p) => p.price < 20) // 2. only cheap ones.map((p) => p.name.toUpperCase()) // 3. only the names in capitals// The same with intermediate steps – sometimes easier to readconst inStock = products.filter((p) => p.inStock)const cheap = inStock.filter((p) => p.price < 20)const names = cheap.map((p) => p.name.toUpperCase())
What you see: Six products (two sold out), a row of buttons with methods and, below them, the code, an explanation and the result. The highlighted products are the ones the method "took". reduce and toSorted are a taste of the next lesson.
Try it:
The takeaway: Before you reach for a method, ask yourself: do I want back an array of the same length (map), a shorter array (filter), one item (find), or yes/no (some/every)?
Loading the interactive part…
| I want… | Method | Returns |
|---|---|---|
| to turn every item into something else | map | a new array of the same length |
| only some items | filter | a new array (shorter or the same) |
| the first item that meets a condition | find | the item, or undefined |
| the position of the first such item | findIndex | a number, or -1 |
| to know whether at least one meets it | some | true / false |
| to know whether all of them meet it | every | true / false |
| to know whether the array contains a value | includes | true / false |
| to do something for every item (no result) | forEach | undefined |
One call of map and one call of filter – you only fill in what the callback returns.
Task is in the comments in the code below – at the top of the file and at the places marked TODO.
Loading the interactive part…
Eight queries over a task list. Each one's array method is already written – you fill in what the callback returns: a value from the task or a condition. The comment at each TODO says what the callback should return. Under the editor you'll see which functions already work.
Task is in the comments in the code below – at the top of the file and at the places marked TODO.
Loading the interactive part…
arr.at(-1), out of range is undefined.includes/indexOf only for simple values; look for objects with some/find by a property.return and ({ … }).!==.undefined – guard it with ?..true.