Array manipulation help, count entries by data and zone

Hello,

First off I apologize for the extremely beginner level question…

I have an array of over 300k entries like so:

array = [{Date_Reported: 2020-5-1, Zone: Edmonton}, {Date_Reported: 2020-5-1, Zone: Calgary},{Date_Reported: 2020-5-1, Zone: Edmonton}…]

I wish to create a new array like so:

newarray = [{Date_Reported: 2020-5-1, Zone: Edmonton, Count: (#of entries that match date and zone)},{Date_Reported: 2020-5-1, Zone: Calgary, Count: (#ofentries)}…]

Combining entries of the same date and zone and increasing the count value for each one found. I have tried for loops, and foreach with if statements with little success and was wondering if there is a more efficient way of approaching this?

Any help provided would be greatly appreciated…

maybe check out d3.group?

1 Like

I looked at group and at rollup and rollups but they either create maps or nested arrays while the visualization functions always use d.objectname to reference array values…

So far data2 = d3.flatRollup(data, v => v.length, d => d.Date_reported, d => d.Zone)
gets me the closest with an array of arrays with the third index being the count I need but I am unsure of how to convert the array of arrays into just an array of objects with keys for the values…

Thank you Fil for putting me one step closer.

Figured it out, for anyone that finds this once you use flatRollup just use another map function with get like this:

datamap = data.map(d => ({
Date: d.Date_reported,
Zone: d.Zone,
Count: data3.get(d.Date_reported).get(d.Zone)

}))
to access the other map and assign the values to the keys of a new array.

This actually didn’t work because I assign the count value without reducing all the dates to one entry… fml

Ok I figured out how to format my data:
Steps:
1)
data2 = d3.flatRollup(data, v => v.length, d => d.Date_reported, d => d.Zone)
2)
datamap = data2.map((d,i) => ({
Date: data2[i][0],
Zone: data2[i][1],
Count: data2[i][2]

}))

This creates the array I was striving for…

1 Like