Chapter Seven · Behaviour

JavaScript: Working with Data

Real data comes in lists of things with named parts. This chapter builds the shapes that hold it, arrays and objects, the four methods that do most of the work with lists, JSON for data that travels, and modules that split the site's code into files.

Chapter 6 worked with one value at a time. Real pages show many things at once, and each thing has several parts: a project has a title, a description, tags and an address, and a portfolio has several projects. This chapter is about the shapes that hold data like that, and the everyday work done with them. By the end of it, the site's three projects live in a module of their own, ready for Chapter 8 to turn them into cards on the page.

Five topics. Arrays, which hold values in order, and loops, which do something with each of them. Objects, which hold values by name, and the list of objects that almost all real data looks like. Four array methods, map, filter, find and sort, and the surprises in the last of them. JSON, the text form data takes whenever it travels or is stored. And modules, which split the site's code into files with import and export, and which work only through the preview server.

From single values to the shape of real data
A list of objects: the shape real data comes in
the site's projects, a page of search results, a forecast
Arrays and objects
arrays hold values in order; objects hold values by name
Values
numbers, strings, booleans, null and undefined

Topics in This Chapter