I would like to transform this:
[
{ a: 2000, b: 4000 },
{ a: 8000, b: 5000 },
{ a: 6000, b: 1000 }
];
Into this:
[
[ 2000, 8000, 6000 ],
[ 4000, 5000, 1000 ]
];
Using Ramda.
I can do this using just R.reduce, but I’m wondering if there’s a way that uses as little custom code as possible and instead makes full use of the functions Ramda provides.
One more caveat; the solution can’t assume that the keys in the objects are known. They will always be consistent between objects, but may change every time this function is run. For example, the next run of the code could be:
Input:
[
{ c: 1000, d: 4000, e: 7000 },
{ c: 2000, d: 5000, e: 8000 },
{ c: 3000, d: 6000, e: 9000 }
];
Result:
[
[ 1000, 2000, 3000 ],
[ 4000, 5000, 6000 ],
[ 7000, 8000, 9000 ],
];
2
Answers
Just use vanilla JS and transpose the mapped object values.
Here is the equivalent in Rambda:
If you can guarantee the same property order in your input objects, then this is quite pretty:
But if you can’t, then you will have to do more work. While this works fine as a point-free version:
I find it less readable than the pointed version:
And, as Mr. Polywhirl points out, with a very simple custom transpose function, this can be done easily enough in vanilla JS.