Some & Every

The some() and every() methods operate on arrays (including arrays of objects).

some()

The some() method will return true if at least one item in the array matches a given condition.

some() applied to an array

let numbers = [1, 3, 6, 8, 11];

let arrayContainsNumber = numbers.some(number => number === 6);

alert(arrayContainsNumber);  // will display true, as the number 6 occurs at least once in the array

 

some() applied to an array of objects

let employees = [{name:"Ann", role:"IT", salary:100000}, 
                 {name:"Brian", role:"HR", salary:50000}, 
                 {name:"Cathy", role:"HR", salary:60000}, 
                 {name:"Dennis", role:"IT", salary:120000}, 
                 {name:"Emma", role:"HR", salary:30000}]; 

let objectContainsNname = employees.some(employee => employee.name === "Cathy");

alert(objectContainsNname);  // will display true, as the name "Cathy" occurs at least once in the object

every()

The every() method will return true if all of the items in the array matches a given condition.

every() applied to an array

let numbers = [1, 3, 6, 8, 11];

let arrayContainsNumber = numbers.every(number => number === 6);

alert(arrayContainsNumber);  // will display false, as the number 6 occurs only least once in the array

 

every() applied to an array of objects

let employees = [{name:"Ann", role:"IT", salary:100000}, 
                 {name:"Brian", role:"HR", salary:50000}, 
                 {name:"Cathy", role:"HR", salary:60000}, 
                 {name:"Dennis", role:"IT", salary:120000}, 
                 {name:"Emma", role:"HR", salary:30000}]; 

let objectContainsName = employees.every(employee => employee.salary > 10000);

alert(objectContainsName);  // will display true, as all of the salaries are at least 10000
 
<div align="center"><a href="file:///C|/Users/Derek/Documents/Notes/versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>