-
-
Save navanathjadhav/29ca758872b94e33bc6813ad9d2558f1 to your computer and use it in GitHub Desktop.
Code example of for in and forEach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Find first manager | |
*/ | |
function findFirstManager(employees) { | |
let manager = {}; | |
employees.forEach((employee) => { | |
if (employee.role === "Manager") { | |
manager = employee; | |
// return or break won't work here, so it will continue to loop till the end | |
} | |
}); | |
// It will return most recent manager NOT first | |
return manager; | |
} | |
/* | |
* Count HRs | |
*/ | |
function countHRs(employees) { | |
let count = 0; | |
// It will loop over employees internal meta properties also | |
for (let i in employees) { | |
if (employees[i].role === "HR") count++; | |
} | |
return count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment