Created
December 22, 2016 11:03
-
-
Save reinisriekstins/ec5ab1750e7cbc3b8260f1f4a91477d2 to your computer and use it in GitHub Desktop.
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
| /*----------------------------------------- | |
| * An example of how it should work | |
| *----------------------------------------- | |
| const obj = { | |
| one: 1, | |
| two: 2, | |
| three: 3, | |
| four: 4 | |
| } | |
| const {one, three, ...rest} = obj | |
| console.log(one) // 1 | |
| console.log(three) // 3 | |
| console.log(rest) // { two: 2, four: 4 } | |
| */ | |
| // implementation: | |
| // obj - the object on which to do the rest operation | |
| // props - array of strings, that contain the property names | |
| // to be excluded from obj | |
| function restifyObject(obj, ...props) { | |
| return Object.keys(obj) | |
| .reduce((acc, key) => { | |
| props.forEach(prop => { | |
| if (props.indexOf(key) === -1) | |
| acc[key] = obj[key] | |
| }) | |
| return acc | |
| }, {}) | |
| } | |
| // example | |
| const obj = { | |
| one: 1, | |
| two: 2, | |
| three: 3, | |
| four: 4 | |
| } | |
| const {one, three} = obj | |
| const rest = restifyObject(obj, 'one', 'three') | |
| console.log(one) // 1 | |
| console.log(three) // 3 | |
| console.log(rest) // { two: 2, four: 4 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment