Skip to content

Instantly share code, notes, and snippets.

@reinisriekstins
Created December 22, 2016 11:03
Show Gist options
  • Select an option

  • Save reinisriekstins/ec5ab1750e7cbc3b8260f1f4a91477d2 to your computer and use it in GitHub Desktop.

Select an option

Save reinisriekstins/ec5ab1750e7cbc3b8260f1f4a91477d2 to your computer and use it in GitHub Desktop.
/*-----------------------------------------
* 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