Skip to content

Instantly share code, notes, and snippets.

@scogle
Created October 6, 2014 19:28
Embed
What would you like to do?
Recursively convert ibeam parent/child output to flare format
function toFlareRecursive(data, parentKey, childKey) {
var root = { 'name': 'Classes', 'children': [] };
function findChildren(d){
var subclasses = _.filter(data, function(obj){
return obj[parentKey] === d[childKey];
});
if ( subclasses.length > 0 ) {
d.children = [];
_.each(subclasses, function(obj){
obj.name = obj.thisClass;
d.children.push(obj);
findChildren(obj);
});
}
}
_.each(data, function(obj){
if ( obj[parentKey] === "None" ) {
obj.name = obj[childKey];
root.children.push(obj);
findChildren(obj);
}
});
return root;
}
@scogle
Copy link
Author

scogle commented Oct 6, 2014

Advantages of this approach

  • All extra data returned by the query is maintained (i.e., color, etc.)
  • Only does what's necessary. Doesn't bother with 'size' key since that's not strictly part of the schema.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment