Skip to content

Instantly share code, notes, and snippets.

@ajcrites
Last active February 19, 2016 16:16
Show Gist options
  • Save ajcrites/996d1f56c448d7a3b7c8 to your computer and use it in GitHub Desktop.
Save ajcrites/996d1f56c448d7a3b7c8 to your computer and use it in GitHub Desktop.
"use strict";
const Company = {
org: "Mobiquity",
employees: [{
title: "Vice President",
employees: [
{
title: "Sales Lead",
employees: [{title: "Salesman"}],
},
{
title: "Marketing Lead",
employees: [{title: "Marketingman"}],
},
],
}],
};
function countEmployees(prev, next) {
// count *this* employee
let sum = prev + 1;
if (next.employees && next.employees.length) {
// count any nested employees
sum += next.employees.reduce(countEmployees, 0);
}
return sum;
}
console.log(Company.employees.reduce(countEmployees, 0));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment