Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mrmanicou opened this issue Jul 24, 2019 · 4 comments
Closed

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

mrmanicou opened this issue Jul 24, 2019 · 4 comments

Comments

@mrmanicou
Copy link

Doesn't seem to work when I do this

@tomi
Copy link
Owner

tomi commented Jul 29, 2019

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

@9ee1
Copy link

9ee1 commented Jan 6, 2020

@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 added a commit that referenced this issue Mar 21, 2020
Internally uses JSON.stringify to "hash" the object and use the hash for equality. I assume this is
sufficient and faster than doing a deep equals check on the object, as JSON.stringify and JSON.parse
are generally fast.

implements #65
tomi added a commit that referenced this issue Mar 21, 2020
Internally uses JSON.stringify to "hash" the object and use the hash for equality. I assume this is
sufficient and faster than doing a deep equals check on the object, as JSON.stringify and JSON.parse
are generally fast.

implements #65
@tomi
Copy link
Owner

tomi commented Mar 21, 2020

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

@tomi tomi closed this as completed Mar 21, 2020
tomi pushed a commit that referenced this issue Mar 21, 2020
# [1.5.0](v1.4.3...v1.5.0) (2020-03-21)

### Features

* **groupby:** add support to use objects as keys in groupBy ([d318187](d318187)), closes [#65](#65)
* export types from the main module ([feee577](feee577))
@9ee1
Copy link

9ee1 commented Mar 22, 2020

Awesome! Thank you very much @tomi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants