Chapter 8. Arrays: putting data into lists

 

This chapter covers

  • Grouping values as lists
  • Creating an array
  • Accessing elements in an array
  • Manipulating the elements in an array
  • Using forEach to visit each element

Almost everything you’ve covered so far has been about organizing your data or organizing your code. This chapter continues that theme but with a twist: it’s not just about grouping items; now you can put them in order.

In The Crypt, you’ll finally have the ability for players to collect items they find on the journey; with arrays they can start on their very own treasure hoards.

8.1. Creating arrays and accessing elements

Working with lists is an essential part of programming. Blog posts, quiz questions, stock prices, emails, files, tweets, and bank transactions all turn up as lists. In fact, you’ve just read a list of lists! Sometimes the order doesn’t matter and sometimes it does. A list of names could represent the members of a team, with no order, or it could represent places in a race—I’m sure Usain Bolt thinks the order is important there! An ordered list is called an array in JavaScript, as it is in many programming languages.

The items in an array are called its elements and you usually want to work on the elements in some way. You might want to

8.2. Array methods

8.3. The Crypt—a player items array

8.4. Summary

sitemap