Skip to content

Instantly share code, notes, and snippets.

@unicornware
Created December 31, 2020 05:08
Show Gist options
  • Select an option

  • Save unicornware/8c15def16d9ec399872bfee7e4ec8e3e to your computer and use it in GitHub Desktop.

Select an option

Save unicornware/8c15def16d9ec399872bfee7e4ec8e3e to your computer and use it in GitHub Desktop.
Reduce an array of objects into a single object
import { merge } from 'lodash'
/**
* @file Implementation - reduceObjects
* @author Lexus Drumgold <[email protected]>
*/
type AnyObject = Record<string, any>
/**
* Merges an array of objects into a single object.
*
* @param args - Objects to merge
*/
const reduceObjects = (...args: AnyObject[]): AnyObject => {
const reducer = (acc: AnyObject, curr: AnyObject) => merge(acc, curr)
return (args || []).reduce(reducer, {})
}
export default reduceObjects
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment