d3/d3-collection

d3.nest reformats a Date object to a string

jason-stein opened this issue · 1 comments

Using d3.nest to aggregate a dataset by date. My code:

vis.nestData = d3.nest()
	.key(function(d){ return d.survey; })
	.rollup(function(leaves){ return leaves.length; })
	.entries(vis.data);

vis.nestData.forEach(function(d){
	d.key = new Date(d.key);
})

vis.data has field survey as JS Date objects. Upon return, the keys become strings, NOT Date objects. The second call is needed to reformat them into dates. Somewhere in the nest call they are converted to strings.

This is the expected (and documented) behavior; keys are strong identifiers, not arbitrary types, so if you try to use a value that is not a string it will be coerced to a string.

This design is partly a consequence of earlier versions of JavaScript lacking the Map class. I think it would be reasonable to switch to Map internally as a breaking major change in the future.