简体   繁体   中英

D3.js create new array with nest() values

I am studying d3.js now and I want to set my dataset for timeseries in js to make a line chart.

I want to group every local and make an array for those occurrence and plot them by date

Right now this is my output

Object {0: Object, 1: Object, 2: Object, 3: Object, 4: Object…}
0: Object
date: "2020-03-08"
local: "MG"
occurrence: 1
1: Object
date: "2020-03-09"
local: "MG"
occurrence: 2
2: Object
date: "2020-03-11"
local: "BH"
occurrence: 1

But I want to set like this

Object{
entries: Array(n) [0: Object {local: "MG", occurrence: Array{1, 2, ..}, 1: Object {local: "BH", occurrence: Array{1 ...}]
date: Array(d) [0: 2020-03-08, 1: 2020-03-09, ...]
}

I am trying to use nest() and rollup() but I want an array with all occurrences but I dont know how to access it because its inside values and then inside occurrence

I manage to solve my own problem thanks to @Mehdi who gives me this link - d3 Grouping and Summarization

What I need to do is to grouping the data by local

data = d3.nest().key(d => d.local)

take all the entries of my occurrence

  .rollup(v => {
    return{
      occur: d3.set(v, d => {
        return d.occurrence;
      }).values()
    }
  })
  .entries(data)

map my data to return just the values I want

  .map(d => {
    return{
      local: d.key,
      occur: d.value.occur
    }
  });

What I not sure is about the map() is if it really elimite all other arrays which was initialized or its somewhere else

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM