Skip to content

Instantly share code, notes, and snippets.

@stordahl
Last active February 28, 2021 03:10
Show Gist options
  • Save stordahl/d5081c68961fdf7a721c93a653c42671 to your computer and use it in GitHub Desktop.
Save stordahl/d5081c68961fdf7a721c93a653c42671 to your computer and use it in GitHub Desktop.
a quick function to merge two arrays of objects where a child from one array share a value (like an ID) with a single child of the other array
// note: to use this function, replace '.id' in line 4
// with the property youre using to match children
const mergeById = (a1: Array<object>, a2: Array<object>) =>
a1.map(itm: object | null => ({
...a2.find(e: object | null => (e.id === itm.id)),
...itm
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment