tomi / fromfrom

A JS library written in TS to transform sequences of data from format to another
MIT License
479 stars 9 forks source link

Can I use an object as a keyselector in a groupBy #65

Closed mrmanicou closed 4 years ago

mrmanicou commented 5 years ago

Doesn't seem to work when I do this

tomi commented 5 years ago

Hi. I'm not sure I understand what you mean. Could you give an example of how you'd want to use groupBy?

9ee1 commented 4 years ago

@tomi - I am also interested in doing this. Here is an example of what I am trying to do. This uses the example data at https://tomi.github.io/fromfrom/playground.html

import { from } from "fromfrom";
import data from "data";

from(data)
.groupBy(d => ({"c": d.country, "g": d.gender}))
.map(d => ({"c": d.key.c, "g": d.key.g, "count": d.items.length}))
.toArray()

Basically I want the number of elements in the sequence for every country and gender combination. But it looks like groupBy cannot compare objects to actually group them so the resulting array ends up flat and looks like this:

[
  {
    "c": "SE",
    "g": "Male",
    "count": 1
  },
  {
    "c": "SE",
    "g": "Male",
    "count": 1
  }
]

Is there a way to accomplish this? Thanks in advance for your help and for your great work on this incredible project!

tomi commented 4 years ago

Hi @9ee1 and thank you for clarifying the use case! I implemented this in #91 and it should be available in the next release.

9ee1 commented 4 years ago

Awesome! Thank you very much @tomi.