Lesson 1.5Lesson 5 of 24
Working with arrays
Given a list of data, demonstrate data manipulation with arrays.
Key points
- 1push, pop, shift, unshift, splice, sort, reverse and fill change the array itself. map, filter, slice and concat return a new array.
- 2sort without a compare function sorts as strings, so 10 comes before 9. Use
(a, b) => a - bfor numbers. - 3Pick the method by the result you need: find gives one item, filter an array, some and every a boolean, reduce one value.
- 4Spread and slice make shallow copies: the new array holds the same objects as the old one.
- 5new Set removes duplicates while keeping the order. A Map keeps the type of its keys, while object keys become strings.
What the exam guide checks here
Objective wording from the exam guide (English).
- 1.5.K1Mutating vs non-mutating array methods (push, splice, sort vs map, filter, slice, concat)
- 1.5.K2Iteration and search: forEach, map, filter, reduce, find, some, every, includes, indexOf
- 1.5.K3Spread, destructuring, Array.from, Set and Map for collections
- 1.5.S1Choose the code that transforms a list into the required result
Lock it in
Answer scenario questions on this topic. Every option comes with an explanation.
Practise 1.5 · 12 questions