Skip to content

Instantly share code, notes, and snippets.

@chloerulesok
Last active November 1, 2017 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chloerulesok/92c221726cd10ba81f6777c712bcc751 to your computer and use it in GitHub Desktop.
Save chloerulesok/92c221726cd10ba81f6777c712bcc751 to your computer and use it in GitHub Desktop.
Combining nests
license: mit

When downloading a large amount of data, it can be necessary to do reduction at various stages in order to not have to hold all of the data at once.

In this block I would like to nest the data using d3's nest function, and then add additional objects to the nest as they come in.

[
{
"name" : "Bob",
"faveColor" : "Red",
"drives" : "BMW",
"birthMonth" : "March"
},
{
"name": "Sam",
"faveColor" : "Red",
"drives" : "Mini",
"birthMonth" : "July"
},
{
"name": "Kate",
"faveColor" : "Blue",
"drives" : "BMW",
"birthMonth" : "March"
},
{
"name": "Kate",
"faveColor" : "Blue",
"drives" : "BMW",
"birthMonth" : "March"
},
{
"name": "Dave",
"faveColor" : "Yellow",
"drives" : "Bike",
"birthMonth" : "August"
}
]
[
{
"name" : "Jill",
"faveColor" : "Pink",
"drives" : "Porsche",
"birthMonth" : "December"
},
{
"name": "Andy",
"faveColor" : "Red",
"drives" : "Mini",
"birthMonth" : "July"
},
{
"name": "Dom",
"faveColor" : "Pink",
"drives" : "BMW",
"birthMonth" : "September"
},
{
"name": "Kate",
"faveColor" : "Blue",
"drives" : "BMW",
"birthMonth" : "March"
},
{
"name": "James",
"faveColor" : "Red",
"drives" : "Ferrari",
"birthMonth" : "July"
}
]
<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<style>
</style>
<body ng-app>
<table id="chart"></table>
</body>
<script src="//d3js.org/d3.v4.min.js"></script>
<script>
// *** Any of the object attributes can be included in here
var nestOrder = ["faveColor","drives", "birthMonth"];
// *** Helper function to create nests dynamically
function createNestingFunction(propertyName){
return function(d){
return d[propertyName];
};
}
// *** Create nesting function
var nest = d3.nest();
for (var i = 0; i < nestOrder.length; i++) {
nest = nest.key( createNestingFunction(nestOrder[i]) );
}
// *** Open files and process
d3.queue()
.defer(d3.json, "dataSet1.json")
.defer(d3.json, "dataSet2.json")
.await(function(error, dataSet1, dataSet2) {
if (error) throw error;
// *** Create nest for dataSet1
var valuesSet1 = nest.entries(dataSet1);
// *** Create nest for dataSet2
var valuesSet2 = nest.entries(dataSet2);
var newNest = [];
joinNests(valuesSet1, newNest);
joinNests(valuesSet2, newNest);
console.log('Finished: ');
console.log(newNest);
});
// *** Helper function to combine nests
// *** Pass in a nest and a nest that is being combined into. Created
// *** in this was so that dynamic numbers of nests can be added
function joinNests(nest1, newNest){
if(nest1.length <= 0){
return;
}
for(var i in nest1){
var found = false;
for(var j in newNest){
if(nest1[i].hasOwnProperty('values')
&& nest1[i]['key'] == newNest[j]['key']){
found = true;
if(nest1[i].hasOwnProperty('values')){
joinNests(nest1[i]['values'],newNest[j]['values']);
}
}
}
if(found == false){
newNest.push(nest1[i]);
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment