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

groupBy on Object: TypeError: this.items.forEach is not a function #313

Open
SDIjeremy opened this issue Jan 21, 2023 · 1 comment · May be fixed by #314
Open

groupBy on Object: TypeError: this.items.forEach is not a function #313

SDIjeremy opened this issue Jan 21, 2023 · 1 comment · May be fixed by #314

Comments

@SDIjeremy
Copy link

SDIjeremy commented Jan 21, 2023

Javascript Collect.js

collect({
  "posts.1.published": "The posts.1.published must be accepted.",
  "posts.1.comments.0.message": "The posts.1.comments.0.message must be a string."
}).groupBy((error, field) => field.substring(0,7))

TypeError: this.items.forEach is not a function
https://runkit.com/embed/ffbieqiw4ine

Laravel Collection

collect([
  "posts.1.published" => "The posts.1.published must be accepted.",
  "posts.1.comments.0.message" => "The posts.1.comments.0.message must be a string."
])->groupBy(fn($error, $field) => substr($field, 0, 7));

Expected Result:

[
  "posts.1" => [
    "The posts.1.published must be accepted.",
    "The posts.1.comments.0.message must be a string.",
  ],
]
SDIjeremy added a commit to SDIjeremy/collect.js that referenced this issue Jan 21, 2023
@SDIjeremy SDIjeremy linked a pull request Jan 21, 2023 that will close this issue
@jjjrmy
Copy link

jjjrmy commented Jan 22, 2023

Kinda annoying because right now the only solution I've found is to map it to an array, then groupBy, then map the groups, and finally map the group's items back into an object with mapWithKeys.

.map((error, field) => ({ error, field })).values()
.groupBy((error) => error.field.substring(0,7))
.map((errors, group) =>
    errors.mapWithKeys((error) => [ error.field.substring(0,7), error.error ])
)

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

Successfully merging a pull request may close this issue.

2 participants