Skip to content

Instantly share code, notes, and snippets.

@nite
Last active July 20, 2021 22:14
Show Gist options
  • Select an option

  • Save nite/7711d69fb5f755239a44fd3232355adf to your computer and use it in GitHub Desktop.

Select an option

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

nite commented Jul 20, 2021

Copy link
Copy Markdown
Author

Faster than the previous lodash implementation

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