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.

Revisions

  1. reinisriekstins created this gist Dec 22, 2016.
    47 changes: 47 additions & 0 deletions restifyObject.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@

    /*-----------------------------------------
    * 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 }