Skip to content

Instantly share code, notes, and snippets.

@potch
Created January 21, 2015 05:00
Show Gist options
  • Select an option

  • Save potch/c4d1ac626c1f69c8bd2c to your computer and use it in GitHub Desktop.

Select an option

Save potch/c4d1ac626c1f69c8bd2c to your computer and use it in GitHub Desktop.
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