Using Array.every()
The method Array.prototype.every()
evaluates all elements in an array to see
if they pass the test given by the provided function. If the array has no values
the method returns true
.
This is very useful when reducing an array to a boolean.
every((element) => {})every((element, index) => {})every((element, index, array) => {})// TypeScriptevery(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean;
A common use of reduce
and filter
is to get a subset of items and then check
if a condition is met. Instead, every
can be used to check that condition
directly.
// Beforeconst foo: boolean = [1, 2, 3].filter((n) => n > 2).length > 0// Orlet foo: boolean = falsefor (const n of [1, 2, 3]) {if (n > 2) {foo = false}}// Afterconst foo: boolean = [1, 2, 3].every((n) => n > 2)