liveBook · Manning

Chapter 9: Chapter 9. Dealing with collections

What you will learn in chapter 9 of Secrets of the JavaScript Ninja, Second Edition

Introduces arrays, maps, and sets by identifying the basic role of each collection.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces arrays, maps, and sets by identifying the basic role of each collection.

Key terms: arrays, maps, sets, dictionaries, keys, values

9.1 Arrays

Introduces arrays as collections and explains that JavaScript arrays are objects, with both benefits and performance costs.

Key terms: arrays, objects, methods

9.1 Do you know?

This section poses three questions to check readers’ prior knowledge of objects as maps, Map key-value types, and Set item types.

9.1.1 Creating arrays

This section explains how to create JavaScript arrays and how indexed access and the length property behave when arrays grow or shrink.

Key terms: array literal, Array constructor, index notation, undefined, array bounds, hole, length property, expand, trim

9.1.2 Adding and removing items at either end of an array

This section explains how four array methods add or remove items at either end and why operations at the end are faster.

Key terms: push, unshift, pop, shift, indexes

9.1.3 Adding and removing items at any array location

This section explains why deleting an array item leaves a hole and how splice removes or inserts items at arbitrary positions.

Key terms: hole, undefined, splice, index, array of items that have been removed, insert, arbitrary positions

9.1.4 Common operations on arrays

This section explains how built-in array methods handle iteration, mapping, condition testing, searching, sorting, and aggregation without manually managing every loop.

Key terms: for loop, forEach, callback, map, mapping an array, every, some, find, filter, indexOf

9.1.5 Reusing built-in array functions

Shows how a normal object can store a collection alongside other state by reusing built-in array methods.

Key terms: normal object, metadata, collection, Array.prototype.push, length property, call method, function context, numbered property, add, gather

9.2 Maps

Introduces maps as key-to-value collections for tasks such as localization, and cautions that using ordinary objects as dictionaries is not generally reliable.

Key terms: key, value, dictionaries, maps, localization, objects, named properties, values, dictionary

9.2.1 Don’t use objects as maps

This section explains why plain JavaScript objects are unreliable as maps: inherited properties can appear as entries, and non-string keys can collapse into the same string key.

Key terms: prototype, constructor property, undefined, string-only keys, toString, [object HTMLDivElement], Map

9.2.2 Creating our first map

This section shows how to create and use a JavaScript Map and explains why separate objects remain distinct map keys even when their contents match.

Key terms: Map constructor, set, get, key, value, mapping, size, has, delete, key equality

9.2.3 Iterating over maps

This section explains how to iterate over a map’s entries, keys, and values, and why entry order is predictable.

Key terms: map, for...of, keys, values, insertion order, for...in

9.3 Sets

Introduces sets as collections of distinct items, shows how they were mimicked with objects before ES6, and explains why ES6 added a dedicated set type.

Key terms: set, distinct items, has, add, remove, prototype objects, ES6, sets

9.3.1 Creating our first set

Introduces how to create a Set, use its basic methods and property, and iterate over its unique items.

Key terms: Set constructor, set, unique items, has, add, size property, for...of loop, insertion order

9.3.2 Union of sets

This section explains what a union contains and how to create one by combining collections and letting a Set remove duplicates.

Key terms: union, spread operator, Set constructor

9.3.3 Intersection of sets

Defines set intersection and explains how to find members shared by two sets using array filtering.

Key terms: intersection, set, filter method, spread operator

9.3.4 Difference of sets

Defines set difference and shows how to select elements of one set that are absent from another.

Key terms: difference of two sets, samurai.has(ninja)

9.4 Summary

9.5 Exercises

Where this is used later

Concepts explained in this chapter

← Chapter 8: Chapter 8. Controlling access to objects Chapter 10: Chapter 10. Wrangling regular expressions →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.