When searching for items in an array, sometimes we search for a single item, other times we search for multiple items that mean some criteria. The Array#find and Array#filter functions are intended to simplify this.
In this recipe, we'll take a look at how to use these two functions to locate elements within an array.
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
10-01-using-find-and-filter
. - Copy or create an
index.html
that loads and runs amain
function frommain.js
. - Create a
main.js
file that defines a new abstractclass
namedRocket
. Assign aname
instance property upon construction:
// main.js class Rocket { constructor(name) { this.name = name; } }
- Create a
main
function that constructs...