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.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 saturnV = new Rocket('US: Saturn V');
const falconHeavy = new Rocket(...