Using Array#every and Array#some to test array values
Sometimes, we need to know information about the array as a whole rather than individual elements, such as "Are there any elements that meet some criterion?" or "Do all the elements meet some criterion?".
In this recipe, we'll take a look at how to use the some and every methods to test 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-03-using-every-and-some-to-test-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...