Last active
February 16, 2017 16:43
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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