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

suggestion: for group by #494

Open
jimmywarting opened this issue Sep 7, 2022 · 0 comments
Open

suggestion: for group by #494

jimmywarting opened this issue Sep 7, 2022 · 0 comments

Comments

@jimmywarting
Copy link

jimmywarting commented Sep 7, 2022

  1. make it acceptable of iterable objects
  2. use Object.create(null) to avoid having to check if xyz is an array using Array.isArray
  3. allow using a 3th argument as a destination object that defaults to an object. (incase you need to use the same object for more stuff)
  4. use ??= assignment to clamp it down even more
function groupBy(iterable, cb, dest = Object.create(null)) {
  if (typeof cb !== 'function') throw new Error('expected a function for second argument')
  if (typeof dest !== 'object') throw new Error('expected a object for third argument')

  for (var item of iterable) (dest[cb(item)] ??= []).push(item)

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

No branches or pull requests

2 participants