Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ========
- // Usage
- // ========
- // > const { transposeObjects } = require('./utils');
- // undefined
- // > transposeObjects([
- // ... { key1: 'object1key1', key2: 'object1key2' },
- // ... { key1: 'object2key1', key2: 'object2key2' }
- // ... ])
- // { key1: [ 'object1key1', 'object2key1' ],
- // key2: [ 'object1key2', 'object2key2' ] }
- const R = require('ramda');
- const lensPropWithDefault = R.curry((defaultValue, key) => {
- return R.lens(R.propOr(defaultValue, key), R.assoc(key));
- });
- const transposeObjects = (objects) => {
- const appendProperty = R.curry((object, acc, key) => {
- return R.over(
- lensPropWithDefault([], key),
- R.append(R.prop(key, object)),
- acc
- );
- });
- const appendProperties = (acc, object) => {
- return R.reduce(appendProperty(object), acc, R.keys(object));
- };
- return R.reduce(appendProperties, {}, objects);
- };
Add Comment
Please, Sign In to add comment