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.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...