object destructuring as code example
Example 1: javascript object destructuring
let obj = {name: 'Max', age: 22, address: 'Delhi'};
const {name, age} = obj;
console.log(name);
console.log(age);
console.log(address);
let a, b, rest;
[a, b] = [10, 20];
console.log(a);
console.log(b);
[a, b, ...rest] = [10, 20, 30, 40, 50];
console.log(rest);
Example 2: destructured object
let renseignement = ['voleur' , '10' , 'spécialité'] ;
let [classe , force, magie] = renseignement ;
console.log(classe) ;
console.log(force) ;
console.log(magie) ;
Example 3: 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 4: mdn destructuring
const x = [1, 2, 3, 4, 5];
const [y, z] = x;
console.log(y);
console.log(z);