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

distinctBy があると便利かもしれない #56

Open
sukobuto opened this issue Sep 16, 2014 · 1 comment
Open

distinctBy があると便利かもしれない #56

sukobuto opened this issue Sep 16, 2014 · 1 comment

Comments

@sukobuto
Copy link
Collaborator

「何を以って重複とするか」を指定できると便利だな、という場面がありました。
例えば「ある親IDに属するメンバーからひとつのみ選択」という条件を適用する場合

$picked_parent_ids = [];
Ginq::from($members)
    ->where(function(Members $m) use (&$picked_parent_ids) {
        if (!in_array($m->parent_id, $picked_parent_ids)) {
            $picked_parent_ids[] = $m->parent_id;
            return true;
        } else {
            return false;
        }
    })

のようにフィルタリングする必要がありますが、これを以下のように表現できるとスッキリします。

Ginq::from($members)
    ->distinctBy('parent_id');
@akanehara
Copy link
Owner

👍

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

No branches or pull requests

2 participants