We just saw how to add values individually to sets. This can be tedious, however. For example, if we are working with a dataset that might be very large or unknown ahead of time, it would be nice to initialize a map with a function call, rather than hundreds or thousands.
In this recipe, we'll take a look at how to create a new Set with the preexisting data.
This recipe assumes that you already have a workspace that allows you to create and run ES modules in your browser. If you don't, refer to the first two chapters.
- Open your command-line application, and navigate to your workspace.
- Create a new folder named
12-02-create-set-from-data
. - Copy or create an
index.html
that loads and runs amain
function frommain.js
. - Create a
main.js
file that defines amain
function with an array of string. Create a new set with that array as a constructor argument:
// main.js export function main() { const rockets = [ 'US: Saturn V', 'US...