Using Array.map to produce values
Other array operations are intended to produce new values. This can be a property from array elements or any other value calculated for each. The map method visits each element and collects the values into a new array.
In this recipe, we'll take a look at how to use map to create an array of new values.
Getting ready
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.
How to do it...
- Open your command-line application, and navigate to your workspace.
- Create a new folder named
10-04-map-to-produce-values. - Create a
main.jsfile that defines a newclassnamedRocketthat takes a constructor argumentnameand assigns it to an instance property:
// main.js
class Rocket {
constructor(name) {
this.name = name;
}
} - Create a
mainfunction that creates severalRocketinstances and places them into an array:
// main.js
export function main() {
const...