Skip to content

JavaScript destructuring rename

  • by

If you want to destructure it change the names then take the same property for restructuring. You can rename one of the fields using this syntax:

const { old_firstName: new_name, lastName } = object

You can assign new variable names like shown in this MDN Example

var o = { p: 42, q: true };

// Assign new variable names
var { p: foo, q: bar } = o;

console.log(foo); // 42
console.log(bar); // true  

JavaScript destructuring rename

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script>
    const person = {
      f: 'Tom',
      l: 'Hank'
    }

    const { f: name, l } = person
    console.log(name,l)

</script>
</body>
</html>

Output:

JavaScript destructuring rename

One more example twitter property to call it tweet. And facebook property to call it fb.

<script>
    const twitter = 'twitter.com';
    const wes = {
      first: 'Tutorial',
      last: 'EyeHunts',
      links: {
        social: {
          twitter: 'https://twitter.com',
          facebook: 'https://facebook.com',
        },
        web: {
          blog: 'https://eyehunts.com'
        }
      }
    };
    const { twitter: tweet, facebook: fb } = wes.links.social;

</script>

Do comment if you have any doubts or suggestions on this Js destructuring topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading