Skip to content

Instantly share code, notes, and snippets.

@monkpit
Created December 8, 2021 23:52
Show Gist options
  • Save monkpit/9808b9f9f0a6e62367bd0398eee5d3d9 to your computer and use it in GitHub Desktop.
Save monkpit/9808b9f9f0a6e62367bd0398eee5d3d9 to your computer and use it in GitHub Desktop.
Merge two objects (not recursive), but only keep properties that already exist on the target object.
const R = require('ramda');
const mergeExistingProps = (target, updater) => {
const targetClone = R.clone(target);
for (const prop in targetClone) {
if (Object.prototype.hasOwnProperty.call(updater, prop)) {
targetClone[prop] = updater[prop];
}
}
return targetClone;
};
module.exports = mergeExistingProps;
@monkpit
Copy link
Author

monkpit commented Dec 8, 2021

Open to ideas of how to make this recursive but also readable 😆 not sure how to quickly check if updater[prop] should trigger recursion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment