Created
June 21, 2016 02:47
-
-
Save zouhenry/a397f54dcf919f5b486bc602784da590 to your computer and use it in GitHub Desktop.
recursive transformer
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 groupBys = ["school", "class", "status"]; | |
var totals = ["notSubmitted", "notGraded"]; | |
var data = getData(); | |
function makeTree(values, level) { | |
level = level || 0; | |
var levelKey = groupBys[level]; | |
var addedVals = {}; | |
if ((level + 1) === groupBys.length) { | |
return getLeafNode(values, levelKey); | |
} else { | |
return _ | |
.chain(values) | |
.map(function(value) { | |
if (addedVals[value[levelKey]]) { | |
return; //prevent same keys being added | |
} | |
addedVals[value[levelKey]] = true; | |
//filter({school:"Chemsford High School"}) | |
var matchingItems = _.filter(values, { | |
[levelKey]: value[levelKey] | |
}); | |
//class | |
var childrenKey = [groupBys[level + 1]]; | |
//obj = {"school": "Chemsford High School", "class":[...]} | |
var obj = { | |
[levelKey]: value[levelKey], [childrenKey]: makeTree(matchingItems, level + 1) // recursion | |
}; | |
_.forEach(totals, function(ttl) { | |
if (obj[ttl] === undefined) { | |
console.log('groupBys[level + 1][ttl]', obj[childrenKey]) | |
obj[ttl] = _.sumBy(obj[childrenKey], ttl) || obj[childrenKey][ttl].length; | |
} | |
}); | |
return obj; | |
}) | |
.compact() //remove falsy values (undefined) | |
.value(); | |
} | |
} | |
function getLeafNode(values, levelKey) { | |
var leaf = _.groupBy(values, levelKey); | |
_.forEach(totals, (totalKey) => { | |
leaf[totalKey] = leaf[totalKey] || []; | |
}); | |
return leaf; | |
} | |
var result = makeTree(data); | |
console.log('result', result); | |
function getData() { | |
return [{ | |
"firstName": "JammieGrp", | |
"lastName": "Hawks", | |
"studentID": "30301", | |
"status": "notGraded", | |
"school": "Chelmsford High School", | |
"class": "11 ABC", | |
"grade": 9 | |
}, { | |
"firstName": "JammieGrp2", | |
"lastName": "Hawks2", | |
"studentID": "30301", | |
"status": "notSubmitted", | |
"school": "Chelmsford High School", | |
"class": "10 EFG", | |
"grade": 9 | |
}, { | |
"firstName": "JammieGrp3", | |
"lastName": "Hawks4", | |
"studentID": "30301", | |
"status": "notSubmitted", | |
"school": "Chelmsford High School", | |
"class": "12 HIG", | |
"grade": 9 | |
}, { | |
"firstName": "1JammieGrp", | |
"lastName": "2Hawks", | |
"studentID": "30301", | |
"status": "notGraded", | |
"school": "Bedford High School", | |
"class": "11 HIG", | |
"grade": 9 | |
}, { | |
"firstName": "JammieGrp2", | |
"lastName": "Hawks2", | |
"studentID": "30301", | |
"status": "notSubmitted", | |
"school": "Bedford High School", | |
"class": "11 HIG", | |
"grade": 9 | |
}, { | |
"firstName": "JammieGrp3", | |
"lastName": "Hawks4", | |
"studentID": "30301", | |
"status": "notSubmitted", | |
"school": "Bedford High School", | |
"class": "11 HIG", | |
"grade": 9 | |
}]; | |
} | |
// function organizedData() { | |
// return [{ | |
// "notSubmitted": 0, | |
// "notGraded": 1, | |
// "total": 1, | |
// "school": "Chelmsford High School", | |
// "classes": [{ | |
// "class": "10 ACSS", | |
// "notSubmitted": 0, | |
// "notGraded": 1, | |
// "total": 1, | |
// "statuses": [{ | |
// "total": 1, | |
// "status": "notGraded", | |
// "students": [{ | |
// "firstName": "JammieGrp", | |
// "lastName": "Hawks", | |
// "studentID": "30301", | |
// "status": "notGraded", | |
// "school": "Chelmsford High School", | |
// "class": "10 ACSS", | |
// "grade": 9 | |
// }] | |
// }] | |
// }] | |
// }]; | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment