Using Array#slice to get a subset of the array
Sometimes, we want a subset of an array based on the array indices rather than on the contents of the array at those indices. In this recipe, we'll take a look at how to use slice to get a subset of an array.
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-02-using-slice-to-get-subset
. - Create a
main.js
file that defines a newclass
namedRocket
that takes a constructor argumentname
and assigns it to an instance property:
// main.js class Rocket { constructor(name) { this.name = name; } }
- Create a
main
function that creates severalRocket
instances and places them into an array:
// main.js export function main() { const saturnV = new Rocket('US: Saturn V'); const falconHeavy = new Rocket(...