Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

How about add groupToArray or chunk? #53

Closed
yuhj86 opened this issue May 3, 2023 · 3 comments
Closed

How about add groupToArray or chunk? #53

yuhj86 opened this issue May 3, 2023 · 3 comments

Comments

@yuhj86
Copy link

yuhj86 commented May 3, 2023

const array = [1, 2, 3, 4, 5];

array.groupToArray((num, index, array) => {
  return Math.floor(index / 2)
});

// or array.chunk(2);
// =>  Array [[1, 2], [3, 4], [5]]
@jridgewell
Copy link
Member

How would this handle non-adjacent runs? Eg:

const array = [1, 2, 3, 1, 2];

array.groupToArray((num, index, array) => {
  return Math.floor(index / 2)
});

// [[1, 2], [3], [1, 2]]
// OR
// =>  Array [[1, 2, 1, 2], [3]]

The first is similar to python's itertools.groupby, and we've discussed exposing a similar API with iterators in committee. But the second effectively the same as Object.values(array.groupBy(…)), and so would be unlikely to be added.

@ljharb
Copy link
Member

ljharb commented May 18, 2023

#44 (comment)

The proposal has been demoted to stage 2, with the expectation that I will be requesting stage 3 for it in July with static methods (#47).

@ljharb ljharb closed this as completed May 18, 2023
@michaelficarra
Copy link
Member

@yuhj86 I plan to propose a chunk helper as a follow-up to the iterator helpers MVP.

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

No branches or pull requests

4 participants