Skip to content

Instantly share code, notes, and snippets.

@xiongchengqing
Last active November 2, 2021 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiongchengqing/95b80020ff859a640d85e8aacc36fea3 to your computer and use it in GitHub Desktop.
Save xiongchengqing/95b80020ff859a640d85e8aacc36fea3 to your computer and use it in GitHub Desktop.
Merge
import mergeWith from 'lodash/mergeWith'
const m1 = { a: 1, b: 2 }
const m2 = { a: 9, c: 3 }
const m3 = { a: 4, b: 5, d: 7 }
// Lodash version
// merge objects with customizer
mergeWith(m1, m2, m3, (objValue, srcValue, key, object) => {
if (Object.prototype.hasOwnProperty.call(object, key)) {
return Array.isArray(objValue) ? [...objValue, srcValue] : [objValue, srcValue]
} else {
return srcValue
}
}) // ?
// Two pointer version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment