Created
September 25, 2017 15:14
-
-
Save ryanSN/16f0c860c8ee4235d1404818b395640c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = pivot | |
function pivot(recordSet) { | |
/* | |
recordSet is an array of records: | |
[{foo: 1311, bar: "cat", zzz: 124.31}, | |
{foo: 41, bar: 11}, | |
{foo: 159, bar: 41, aaa: 12, zzz: 55}] | |
The goal is to pivot this structure: | |
{foo: [1311, 41, 159], | |
bar: ["cat", 11, 41], | |
zzz: [124.31, 55], | |
aaa: [12]} | |
*/ | |
var pivoted = {} | |
if (recordSet == null) return pivoted | |
recordSet.forEach(function (record) { | |
Object.keys(record).forEach(function (key) { | |
if (!pivoted[key]) pivoted[key] = [] | |
pivoted[key].push(record[key]) | |
}) | |
}) | |
return pivoted | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment