Last active
February 19, 2016 16:16
-
-
Save ajcrites/996d1f56c448d7a3b7c8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
"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