A group_by filter for liquid-node
/Here’s a group_by filter for liquid-node
// https://stackoverflow.com/a/34890276/40857 const groupBy = function(xs, key) { return xs.reduce(function(rv, x) { (rv[x[key]] = rv[x[key]] || []).push(x); return rv; }, {}); }; // https://github.com/docs/liquid#registering-new-filters engine.registerFilters({ group_by: (input, key) => { const result = groupBy(input, key); // map dictionary back to arry return Object.keys(result).map(key1=>{ return { key: key1, items: result[key1] }; }); } });
Use this like so
{% assign grouped = data | group_by:'user' %} {% for group in grouped %} <ul>{{group.key}} {% for item in group.items %} <li>{{item.name}}</li> {% endfor %} </ul> {% endfor %}