how to destructure parameter in js code example
Example 1: javascript function destructuring
function f() {
return [1, 2];
}
let a, b;
[a, b] = f();
console.log(a);
console.log(b);
Example 2: how destructuring works in javascript
const objA = {
prop1: 'foo',
prop2: {
prop2a: 'bar',
prop2b: 'baz',
},
};
const { prop1, prop2: { prop2a, prop2b } } = objA;
console.log(prop1);
console.log(prop2a);
console.log(prop2b);
Example 3: array destructuring methods parameters
const items = [ ['foo', 3], ['bar', 9] ];
items.forEach(([word, count]) => {
console.log(word+' '+count);
});
Example 4: array destructuring methods parameters
function foo([a, b]) {
console.log(`param1: ${a}, param2: ${b}`);
}