Skip to content

Instantly share code, notes, and snippets.

@nite
Last active July 20, 2021 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nite/7711d69fb5f755239a44fd3232355adf to your computer and use it in GitHub Desktop.
Save nite/7711d69fb5f755239a44fd3232355adf to your computer and use it in GitHub Desktop.
typescript pivot
function pivot(meltedData, pivotCols: string[], varName: string, valueName: string) {
const groups = {};
for (const d of meltedData) {
const key = pivotCols.map((k) => d[k]).join('|');
const group = groups[key];
groups[key] = {
...(group ? group : fromPairs(pivotCols.map((k) => [k, d[k]]))),
[d[varName]]: d[valueName],
};
}
return Object.values(groups);
}
@nite
Copy link
Author

nite commented Jul 20, 2021

Faster than the previous lodash implementation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment