Skip to content

Instantly share code, notes, and snippets.

@baetheus
Last active August 23, 2017 04:42
Show Gist options
  • Select an option

  • Save baetheus/006829ca6c417d846af03da08c733235 to your computer and use it in GitHub Desktop.

Select an option

Save baetheus/006829ca6c417d846af03da08c733235 to your computer and use it in GitHub Desktop.
Scan and lodash
/*
This will turn your arrays into a keyed object like:
{
1: <Obj 1>,
2: <Obj 2>,
3: <Obj 3>
}
Newest values will over-write old values.
*/
const keyedSource = source.scan((acc, arr) => _.assign(acc, _.keyby(arr, 'id'));
/*
This will take that keyed object and map it to an array like:
[<Obj 1>, <Obj 2>, <Obj 3>]
*/
const backToArray = keyedSource.map(_.values);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment