Working with Arrays
Indexes, length, searching, slicing, and the array of objects behind every list.
An object gives values names. An array gives them order, which is what a list of shows needs.
Making an array and reading it
Square brackets, values separated by commas. Positions start at 0.
length is the number of items, so the last index is length - 1. Asking for
titles[10] prints undefined. Reading past the end is not an error — the
crash comes later, when something tries to use that undefined.
Counting from the end
Brackets cannot count backwards. titles[-1] looks for a property named
"-1", and there is no such property.
at accepts a negative position and counts back from the end. Use it whenever
you want the last item.
Adding and removing at the ends
push adds an item to the end. pop removes the last one and hands it back.
push returns: 3, because that is the new length. To see the array, log the
array.
Both methods change titles in place. It is a const, and it still changed:
const protects the name, not the value, as
let, const, and Scope shows. React wants a new array
instead of a changed one, and
Destructuring, Spread, and Rest covers how to
copy.
Searching
For plain values, includes answers yes or no, and indexOf gives a
position.
indexOf gives -1 when there is no match. That is a number, and -1 is
truthy, so never test a search with if (titles.indexOf(title)).
Finding a show
Both methods above compare with ===, so they cannot find an object by its
fields. find takes a function instead, and gives you the first item that
function says yes to.
bear is the whole show object, so bear.year is 2022. Nothing matched
"Andor", so andor is undefined. Reading andor.year would throw, which
is why the last line uses ?. from Working with Objects.
Taking a section
slice copies a range: from the first index, up to but not including the
second.
slice(0, 2) gives two titles, not three: index 2 is where it stops. With
one argument it runs to the end, and a negative one counts back from the end.
The original still has all five, unlike after a push or a pop.
An array of objects
This is the shape every list on screen is built from: an array, each item an
object, each object carrying an id.
Two steps happen on shows[1].title: read the item, then read its field. When
the item is missing, the second step throws a TypeError, and ?. stops it.
The id is not decoration. When React renders this list, it uses the id as
each row's key, so state stays attached to the right show.
Sorting compares text by default
sort turns each item into text before comparing, so 10 lands before 2.
The sorted line reads [1, 10, 2], because "10" comes before "2" as text.
To sort numbers you pass sort a function that compares two of them, for
example (a, b) => a - b.
Common mistakes
- Using
titles[-1]. Brackets do not count backwards;at(-1)does. - Searching objects with
indexOforincludes. They compare with===, so pass a function tofindinstead. - Reading a field straight off a
find. It givesundefinedwhen nothing matches, so use?.or check first.
Exercise
Three lines, three wrong answers. Fix them so the output reads:
Next up: The Bear
Position: 1
Last: Shogun
The first prints the whole show instead of its title. The second searches a list of titles for a show object. The third uses a negative index the way brackets do not support.