Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

269
Views
How to pass keys as a list of strings to D3 group?

The d3.group documentation says:

d3.group(iterable, ...keys)

For example, if I have the following:

data = [
  {name: "jim",   amount: "34.0",   date: "11/12/2015"},
  {name: "carl",  amount: "120.11", date: "11/12/2015"},
  {name: "stacy", amount: "12.01",  date: "01/04/2016"},
  {name: "stacy", amount: "34.05",  date: "01/04/2016"}
]

But instead of:

d3.group(data, d => d.name, d => d.date)

I want to do something like:

d3.group(data, ['name', 'date'])

How should I do it? The reason is that I might not know the keys in advance and will have to do the grouping programatically.

Thank you!

almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

...keys can be a map which itself returns a function e.g. ...keys.map(k => d => d[k]).

See below - this uses groups instead of group as it's readable in the console output - but this works for group as well.

const data = [
  {name: "jim",   amount: "34.0",   date: "11/12/2015"},
  {name: "carl",  amount: "120.11", date: "11/12/2015"},
  {name: "stacy", amount: "12.01",  date: "01/04/2016"},
  {name: "stacy", amount: "34.05",  date: "01/04/2016"}
];

const keys = ["name", "date"];

const grps = d3.groups(
  data,
  ...keys.map(k => d => d[k])
);

console.log(grps);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.6.1/d3.min.js"></script>

almost 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!