Skip to content

Instantly share code, notes, and snippets.

@vvatikiotis
Last active October 18, 2016 04:53
Show Gist options
  • Save vvatikiotis/2f6b64da8481d7dd423ee3666c6e53fb to your computer and use it in GitHub Desktop.
Save vvatikiotis/2f6b64da8481d7dd423ee3666c6e53fb to your computer and use it in GitHub Desktop.
// Takes an array of objects and turns it into an object where:
// - keys are specified by the key argument
// - values are the the initial object, minus the specified key
// Example:
// const arr = [
// { id: 1, name: 'blah', address: 'qwerqwer' },
// { id: 2, name: 'blah', address: 'qwerqwer' },
// { id: 6, name: 'blah', address: 'qwerqwer' },
// ]
//
// turns into
//
// {
// "1": {name: 'blah', address: 'qwerqwer'},
// "2": {name: 'blah', address: 'qwerqwer'},
// "6": {name: 'blah', address: 'qwerqwer'},
// }
// REVIEW: needs a proper name
export const arrOfObjToKeyObj = curry((key, arr) => {
compose(
mergeAll,
map(v => ({ [prop(key, v)]: omit([key], v) }))
)(arr)
})
// Maps over the object produced by 'arrOfObjToKeyObj'
// Plays nice when iterating with JSX
export const mapObject = curry((fn, obj) => compose(map(key => fn(key, obj[k])), keys)(obj))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment