Skip to content

Instantly share code, notes, and snippets.

@malonehedges
Created October 22, 2016 07:40
Show Gist options
  • Save malonehedges/fccad50ba8b5a4f8ecf2ab71d87a287d to your computer and use it in GitHub Desktop.
Save malonehedges/fccad50ba8b5a4f8ecf2ab71d87a287d to your computer and use it in GitHub Desktop.
export default let mergeByObjectProperty = (mainArray, mergingArray, property) => {
// For every entry in the merging array (smaller)
_.each(mergingArray, (mergingArrayEntry) => {
// Find the corresponding object from arrayOne
var mainArrayEntry = _.find(mainArray, (arr1obj) => {
return arr1obj[property] === mergingArrayEntry[property]
})
// If there already exists this object in the main array
if (mainArrayEntry) {
// Merge the objects
_.extend(mainArrayEntry, mergingArrayEntry)
} else {
// Otherwise (not in the main array), add it!
mainArray.push(mergingArrayEntry)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment