There is plenty of documentation on how to destructure objects passed as function parameters in Javascript 2015 / ES6 / ECMAScript 2015, with a function like this:
function foo({a, b}) { console.log(`a: ${a}, b: ${b}`); }
But how do you destructure an array parameter?
The Syntax with Destructuring. var first = "laide", second = "Gabriel", third = "Jets"; The Syntax Without Destructuring. You cannot use Numbers for destructuring.
Destructuring means to break down a complex structure into simpler parts. With the syntax of destructuring, you can extract smaller fragments from objects and arrays. It can be used for assignments and declaration of a variable.
Destructuring in JavaScript is used to unpack or segregate values from arrays or properties from object literals into distinct variables, thus it allows us to access only the values required.
JavaScript Object Destructuring is the syntax for extracting values from an object property and assigning them to a variable. The destructuring is also possible for JavaScript Arrays.
The correct syntax to destructure an array parameter is:
function foo([a, b]) { console.log(`param1: ${a}, param2: ${b}`); }
It can be called like this:
foo(['first', 'second']); // Will output: // param1: first, param2: second
According to Exploring ES6, section 11.6, you can use this to destructure parameters within arrow functions as well:
const items = [ ['foo', 3], ['bar', 9] ]; items.forEach(([word, count]) => { console.log(word + ' ' + count); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With