Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@navanathjadhav
Created June 12, 2022 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save navanathjadhav/29ca758872b94e33bc6813ad9d2558f1 to your computer and use it in GitHub Desktop.
Save navanathjadhav/29ca758872b94e33bc6813ad9d2558f1 to your computer and use it in GitHub Desktop.
Code example of for in and forEach
/*
* 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