-
Notifications
You must be signed in to change notification settings - Fork 639
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 complex attributes #1198
Comments
Oh man, this is exactly the problem I'm trying to solve. |
@mattradford That's exactly what I was referring to with |
Looking at the underlying It takes a function, so you can write a generic getPath function that returns a function to get the value you want to group on. Just verified that this works. Here's my const getPath = (path) => {
const parts = path.split('.');
return (value, i) => {
let o = value;
for (const p of parts) {
if (o == null) {
return;
}
o = o[p];
}
return o;
};
} |
@justinfagnani The function is straight forward - but the question is how this should look like in the expression. |
The depends on how you get the function as data into your template. I'm using eleventy, and putting the function into a {% for group, examples in collections.examples | groupby(docs.getPath('data.group')) %}
...
{% endfor %} |
This is a filter I'm using for Nunjucks in Eleventy:
Sample (actual) usage:
Acknowledgement: Code based on whatterz/includes.js |
@anaurelian that's already pretty slick - but it would need a special case for dates I guess. |
I have a list of objects. These objects have a
date
attribute. I want to group by year.Something along the lines of the following would be nice.
But it begs the question how to deal with transforms that need to happen before the grouping.
Is there a way to use
groupby
like this?The text was updated successfully, but these errors were encountered: