Tip 15: Create Maps Without Side Effects

In this tip, you’ll learn how to avoid side effects by creating new maps from an array of pairs.

Copying & mutation in maps

Up to this point, you’ve always worked on a single instance of a map. You’ve either added data or removed data directly from an instance of a Map object.

Working on the instance of a map can lead to a few problems. How do you create copies of a map? How can you make changes without side effects?

Fortunately, you can solve those problems by applying a few principles you’ve learned from arrays and objects.

To start, look at an example that combines the problems of copying and mutations: applying a set of defaults to a map.

Example: Applying defaults to a map

In your pet adoption code, you have filters that users have selected, but perhaps you want to add a set of default filters. Any additional filters will be overridden by the user, but any not explicitly set by the user will be the default.

Get hands-on with 1200+ tech skills courses.