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)
data
list of objects - data to meltkeep
array of strings - names of object keys to keepvarName
string - name of the key to use in the molten objects for the former keys (those not in keep)valName
string - name of the key to use in the molten objects for the former valuesnoAddId
bool - if truthy, don't add the _id value to the resulting object
cast(data, keep, fun, funArgs ...)
data
list of objects - data to castkeep
array of strings - names of object keys to keep (the "GROUP BY" keys)fun
function - a function used to aggregate values accross grouped objectsfunArgs
mixed - arguments to passed tofun
cast.sum(name, cols) - convience function to summarize data with cast
name
string name of resulting object key with sum resultscols
string 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
name
string - special case ofcast.sum(name, cols)
name is treated as bothname
andcols
.
cast.count(name) - convience function to count data with cast
name
string - name of resulting object key with which to count objects with commonkeep
values
Apache 2