I'm a bit novice/moderate at JS. To my surprise I managed to put this together and it does work for what I intended.
The objective is to convert an object into a format that is more conducive to being used with a treegrid. I'm using the underscore library to grab object keys at various points.
I just wanted to know if maybe there's a better or perhaps more performant way of looping through these deeply nested objects that I may be overlooking. Something that's capable of handling hundreds, or even a thousand, of these nodes without potentially locking up a browser?
I found a hint about storing the length variables separately in a similar question that displayed looping through things in this manner, I just wanted to know if there may be any other tips?
var data_source = {"sprint":{"children":{"country":{"us":{"clicks":26}},"device":{"iphone":{"clicks":26}},"isp":{"comcast cable":{"clicks":26}},"os":{"ios":{"clicks":15},"android":{"clicks":10}},"referer":{"unknown":{"clicks":26}}},"clicks":26},"verizon":{"children":{"country":{"us":{"clicks":10,"conversions":5,"earned":0.5,"spent":0.2}},"device":{"galaxy s":{"clicks":1}},"isp":{"comcast cable":{"clicks":1}},"os":{"android":{"clicks":1}},"referer":{"unknown":{"clicks":1}}},"clicks":1,"conversions":1}};
var object = [];
function convert(data){
var keys = _.keys(data);
var keyslength = keys.length;
for (var i = 0; i < keyslength; i++){
var me = data[keys[i]];
var rootleaf = buildLeaf(me);
rootleaf.name = keys[i];
rootleaf.children = [];
var childkeys = _.keys(me.children);
var childkeyslength = childkeys.length;
for (var i2 = 0; i2 < childkeyslength; i2++){
var childme = me.children[childkeys[i2]];
var childleaf = {};
childleaf.name = childkeys[i2];
childleaf.children = [];
var grandchildkeys = _.keys(childme);
var grandchildkeyslength = grandchildkeys.length;
for (var i3 = 0; i3 < grandchildkeyslength; i3++){
var grandchildme = childme[grandchildkeys[i3]];
var grandchildleaf = buildLeaf(grandchildme);
grandchildleaf.name = grandchildkeys[i3];
childleaf.children.push(grandchildleaf);
}
rootleaf.children.push(childleaf);
}
object.push(rootleaf);
}
}
convert(data_source);
console.log(object);
function buildLeaf(node){
var tempnode = {};
var clicks = ((node.clicks) ? node.clicks : 0);
var conversions = ((node.conversions) ? node.conversions : 0);
var earned = ((node.earned) ? node.earned : 0);
var spent = ((node.spent) ? node.spent : 0);
tempnode.clicks = clicks;
tempnode.conversions = conversions;
tempnode.earned = '$' + earned.toFixed(2);
tempnode.spent = '$' + spent.toFixed(2);
tempnode.conversion_rate = conversionRate(conversions,clicks);
tempnode.net_earned = netEarned(earned,spent);
tempnode.epc = epc(clicks,earned,spent);
tempnode.roi = roi(earned,spent);
return tempnode;
}
// calculation functions
function conversionRate(cv, cl) {
if (cl === 0) return '0%';
return ((cv/cl)*100).toFixed(1) + '%';
}
function epc(cl, e, s) {
if (cl === 0 || e === 0) return '$0.000';
return '$' + ((e - (cl * s)) / cl).toFixed(3);
}
function netEarned(e, s) {
if (e === 0) return '$0.00';
return '$' + (e - s).toFixed(2);
}
function roi(e, s) {
if (e === 0) return '0%';
return (((e - s) / s) * 100).toFixed(0) + '%';
}
Here's a jsbin link for posterity: http://jsbin.com/uwudaw/3/edit
Thanks for any advice.