Announcement

Collapse
No announcement yet.

Can you give an example for destructuring an object or an array?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can you give an example for destructuring an object or an array?

    Can you give an example for destructuring an object or an array?


    Destructuring is an expression available in ES6 which enables a succinct and convenient way to extract values of Objects or Arrays and place them into distinct variables.



    Array Destructuring

    Code:
    // Variable assignment.
    const foo = ['one', 'two', 'three'];
    const [one, two, three] = foo;
    
    console.log(one); // "one"
    console.log(two); // "two"
    console.log(three); // "three"



    Object Destructuring

    Code:
    // Variable assignment.
    const o = { p: 42, q: true };
    const { p, q } = o;
    
    console.log(p); // 42
    console.log(q); // true

Related Topics

Collapse

Working...
X