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

Method reduce with key/index #311

Open
SDIjeremy opened this issue Jan 16, 2023 · 3 comments · May be fixed by #312
Open

Method reduce with key/index #311

SDIjeremy opened this issue Jan 16, 2023 · 3 comments · May be fixed by #312

Comments

@SDIjeremy
Copy link

SDIjeremy commented Jan 16, 2023

Currently it is not possible to access the Key/Index of the item while using reduce()

$collection->reduce(function ($carry, $value, $key) use ($ratio) {
    return $carry + ($value * $ratio[$key]);
});

https://laravel.com/docs/9.x/collections#method-reduce

@SDIjeremy SDIjeremy changed the title Method reduce with key Method reduce with key/index Jan 16, 2023
@SDIjeremy SDIjeremy linked a pull request Jan 16, 2023 that will close this issue
@ecrmnn
Copy link
Owner

ecrmnn commented Jan 18, 2023

Hi.
Thanks for reporting.
Could you please provide an example on Runkit?

https://npm.runkit.com/collect.js

@jjjrmy
Copy link

jjjrmy commented Jan 19, 2023

Hi. Thanks for reporting. Could you please provide an example on Runkit?

https://runkit.com/embed/t67a6zx0ng4g

Notice how the Object logs the keys, but the Array does not.
I understand that there are no keys on an array, but we should get the indexes instead.

@SDIjeremy
Copy link
Author

Can you please check my PR, it says it failed but I can't see the 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.

3 participants