Testing for an Array Element
JavaScript provides a method on arrays called every which will test every element for a specific test. If all pass the test, overall every method will pass true. If one or more fail the test, then overall every method will return false.
Checking every element of an array for a test
Suppose we have an array where we want to test if every number is above 15;
let arr = [ 25, 35, 45, 55, 65 ];
let check = arr.every((el, index, array) => {
return el > 15
});
console.log(check); // true
every takes a function in the form (el, index, array) => ...
el
is the current element being iterated through by every.index
is the index of the current element being iterated through by the array.array
is the full array being iterated upon. Here it would be[ 25, 35, 45, 55, 65 ]
.
Since array functions will return true, the above code can be simplified to this:
let arr = [ 25, 35, 45, 55, 65 ];
let check = arr.every((el, index, array) => el > 15);
console.log(check); // true
The every
method can take another form where it accepts a callback function's name, along with a customs value for this
. That means you can create a function, and then prepare its content based on a custom this
within the function itself.
For example, below I pass {value: 15}
into this
, allowing us to change this value should we want to when we call every
:
let callbackFn = function(el) {
if(this.value !== undefined) {
return el > this.value
}
return false;
}
let arr = [ 25, 35, 45, 55, 65 ];
let check = arr.every(callbackFn, { value: 15 }); // Returns true
Modifying an array with the every method
Modifying an array with the every method
JavaScript allows modification of the array within a method called upon it. For example, we could modify each element of our array and then do a check on it. Since every
is essentially a type of loop, each element is looped over sequentially:
let arr = [ 25, 35, 45, 55, 65 ];
let check = arr.every((el, index, array) => {
arr[index] -= 100;
return el > 15
});
// This is true, since we subtracted 100 from each element before the check
console.log(check); // true
We can delete elements from an array in the every
loop:
let arr = [ 25, 35, 45, 55, 65 ];
let check = arr.every((el, index, array) => {
arr.pop();
return el > 15;
});
console.log(arr); // returns [ 25, 35 ]
Javascript lets you add elements to an array in every
. Since every
fires only once for each element in an array, it cannot lead to an infinite loop by simply pushing elements to the original array:
let arr = [ 25, 35, 45, 55, 65 ];
let check = arr.every((el, index, array) => {
arr.push(100);
return el > 15;
});
// This is true, since we subtracted 100 from each element before the check
console.log(arr); // returns [25, 35, 45, 55, 65, 100, 100, 100, 100, 100]
console.log(check); // returns true
Conclusion
The every
method is a built-in way to do logical tests on entire arrays. It’s a useful tool to add to your array arsenal but you should be careful with it on large arrays as it may lead to performance problems.
Happy coding!