Created
January 21, 2015 05:00
-
-
Save potch/c4d1ac626c1f69c8bd2c 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
| var staff = require('./phonebook.json'); | |
| staff = staff.map(function (e) { | |
| return { | |
| cn: e.cn, | |
| dn: e.dn, | |
| manager: e.manager, | |
| title: e.title | |
| }; | |
| }); | |
| var root = staff.filter(function (e) { | |
| return e.cn.indexOf('Baker') > -1; | |
| })[0]; | |
| root.walked = true; | |
| getReports(root); | |
| function getReports(node) { | |
| node.reports = staff.filter(function (e) { | |
| if (!e.walked && e.manager && e.manager.dn === node.dn) { | |
| e.walked = true; | |
| return true; | |
| } | |
| return false; | |
| }); | |
| node.reports.forEach(getReports); | |
| } | |
| var depth = 0; | |
| function walk(node, callback) { | |
| depth++; | |
| var middle = false; | |
| node.reports.forEach(function (e) { | |
| if (e.reports.length) { | |
| middle = true; | |
| } | |
| }); | |
| callback(node); | |
| if (node.reports.length) { | |
| node.reports.forEach(walk); | |
| } | |
| depth--; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment