Javascript library inspired by the R reshape package by Hadley Wickham.
Melt provides two functions melt and cast. How do they work?
First, they presume you have your data in the form of a list of objects. That looks like this:
var data = [
{key1: 1, key2: 2},
{key1: 2, key2: 3},
{key1: 3, key2: 5},
];But if you need the keys to be represented as a value, then melt can come to the rescue. Let's take a look at some examples:
Melt: jsfiddle
Cast: jsfiddle
melt(data, keep, varName, valName, noAddId)
datalist of objects - data to meltkeeparray of strings - names of object keys to keepvarNamestring - name of the key to use in the molten objects for the former keys (those not in keep)valNamestring - name of the key to use in the molten objects for the former valuesnoAddIdbool - if truthy, don't add the _id value to the resulting object
cast(data, keep, fun, funArgs ...)
datalist of objects - data to castkeeparray of strings - names of object keys to keep (the "GROUP BY" keys)funfunction - a function used to aggregate values accross grouped objectsfunArgsmixed - arguments to passed tofun
cast.sum(name, cols) - convience function to summarize data with cast
namestring name of resulting object key with sum resultscolsstring or array - names of keys to totaled in the sum. Key values should respond to+.
cast.sum(name) - convience function to sum data with cast
namestring - special case ofcast.sum(name, cols)name is treated as bothnameandcols.
cast.count(name) - convience function to count data with cast
namestring - name of resulting object key with which to count objects with commonkeepvalues
Apache 2