Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gilles-gardet/62bfd6b14b4910a5db573de9f420845d to your computer and use it in GitHub Desktop.
Save gilles-gardet/62bfd6b14b4910a5db573de9f420845d to your computer and use it in GitHub Desktop.

Revisions

  1. gilles-gardet created this gist Apr 3, 2022.
    13 changes: 13 additions & 0 deletions Object destructing with dynamic key.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    // we can also give alias name of variable when destructuring

    const productData = {id: 23, name: 'Laptop'}
    const {name: deviceName} = productData

    console.log('deviceName', deviceName) // deviceName Laptop

    // here destructuring value with dynamic key
    const extractKey = 'name'
    const { [extractKey]: data } = productData

    console.log('data', data) // data Laptop