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

d3.index(iterable, …keys)? #136

Closed
mbostock opened this issue Feb 6, 2020 · 2 comments · Fixed by #157
Closed

d3.index(iterable, …keys)? #136

mbostock opened this issue Feb 6, 2020 · 2 comments · Fixed by #157

Comments

@mbostock
Copy link
Member

mbostock commented Feb 6, 2020

Sometimes you want something like d3.group, but with the expectation that there will be a single value rather than multiple values per unique key. You can do this using d3.rollup as

d3.rollup(data, ([d]) => d, d => d.foo, d => d.bar)

but this is a little verbose, and it will silently ignore additional values if the keys happen to be non-unique. So how about a stricter shorthand?

d3.index(data, d => d.foo, d => d.bar)

Where:

function index(values, ...keys) {
  return nest(values, identity, indexReduce, keys);
}

function indexReduce(values) {
  if (values.length !== 1) throw new Error("non-unique key");
  return values[0];
}

Is d3.index too generic a name? Maybe d3.groupUnique?

(See also the previous d3.uniques proposal #118.)

@mbostock
Copy link
Member Author

mbostock commented Feb 6, 2020

@Fil
Copy link
Member

Fil commented Feb 6, 2020

Maybe the word could be smth like "associate" or "dict / dictionary".

(I tend to rollup with v => v.pop().)

@Fil Fil mentioned this issue Jun 24, 2020
Fil added a commit that referenced this issue Jun 25, 2020
mbostock pushed a commit that referenced this issue Aug 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants