Skip to content

Instantly share code, notes, and snippets.

@marlun78
Last active February 16, 2017 16:43
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 marlun78/982b6767a70921e80d0cb382c6895930 to your computer and use it in GitHub Desktop.
Save marlun78/982b6767a70921e80d0cb382c6895930 to your computer and use it in GitHub Desktop.
Move properties and their values from one object into another by mutating the source object.
// const source = {name: 'Martin', age: 38};
// const target = moveProps(/name/, source);
// console.log(source, target); => {age: 38}, {name: 'Martin'}
export default function moveProps(pattern, source, target = {}) {
return Object.keys(source).reduce((accumulator, key) => {
if (pattern.test(key)) {
accumulator[key] = source[key];
delete source[key];
}
return accumulator;
}, target);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment