Skip to content

Instantly share code, notes, and snippets.

@ayende

ayende/i3.js Secret

Created Sep 9, 2021
Embed
What would you like to do?
// map
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
map("Orders", o => {
var results = [];
for(var i = 0; i < o.Lines.length; i++){
var cur = {
Product: o.Lines[i].Product,
Year: new Date(o.OrderedAt).getFullYear(),
};
var month = monthNames[new Date(o.OrderedAt).getMonth()];
cur[month + " Sales"] = o.Lines[i].Quantity;
cur[month + " Totals"] = o.Lines[i].Quantity * o.Lines[i].PricePerUnit * (1-o.Lines[i].Discount)
results.push(cur);
}
return results;
})
// reduce
groupBy(x => ({Product: x.Product, Year: x.Year}))
.aggregate(g=>{
var final = {};
for(var i = 0; i < g.values.length; i++) {
var keys = Object.keys(g.values[i]);
for(var j = 0; j < keys.length; j++) {
if (keys[j].endsWith("Sales") == false && keys[j].endsWith("Totals") == false)
continue;
final[keys[j]] = (final[keys[j]] || 0) + g.values[i][keys[j]];
}
}
final.Product = g.key.Product;
final.Year = g.key.Year;
return final;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment