javascript
Test every item of array against some condition in javascript
We can test every value of an array against some condition using Array.every() method and if all the items satisfy the condition, it will return true.
const scores = [86, 70, 56, 52, 78, 83];
const is_passed = scores.every( score => score > 33 );
console.log(is_passed);
// -> true
Output
true
In the above code snippet, we have an array of scores that contains marks obtained by a student. If all marks are greater than 33 then the student is passed. It will return true if the student is passed. And false if the student is failed.
Was this helpful?
Similar Posts
- Lodash - Get index of array item based on some condition
- Run a function every 5 seconds interval using Javascript
- Lodash - Create a string from array elements join with some separator
- Render item condition based React
- Stop loop for some seconds in Javascript
- Array.find() - get the very first element satisfy a condition Javascript
- Add new item at first position of array using unshift() method Javascript