Skip to content

Instantly share code, notes, and snippets.

@peterdresslar
Last active October 6, 2020 03:00
Show Gist options
  • Save peterdresslar/a8500263b34b3ab31f1471229703c767 to your computer and use it in GitHub Desktop.
Save peterdresslar/a8500263b34b3ab31f1471229703c767 to your computer and use it in GitHub Desktop.
Quick personal lodash tester
//Credit: https://stackoverflow.com/questions/25047463/group-by-and-sum-using-underscore-lodash/45287338
var _ = require('lodash');
const data = [
{
shape: 'foo',
sides: 15,
len: 4
},
{
shape: 'foo',
sides: 12,
len: 3
},
{
shape: 'foo',
sides: 12,
len: 1
},
{
shape: 'bar',
sides: 6,
len: 5
},
{
shape: 'baz',
sides: 10,
len: 1
}
];
const T = 2;
var output = _(data)
.groupBy('shape')
.map((s, id) => ({
shape: id,
avgOfSides: _.meanBy(s, 'sides'),
countSides: Object.values(_.countBy(s, 'shape'))[0]
}))
.orderBy('avgOfSides', 'desc')
.value();
_.filter(output, function(o) {return o.countSides > T})
console.log(output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment