Friday, January 6, 2023

Destructuring Objects

 const trailblazer = {

    name : 'MK',
    address : {
        state : 'Karnataka',
        country : 'India'
    },
    certifications : ['Admin','PD I','PD II', 'Application Architect'],
    trailhead : {
        badges : 115,
        points : 90725,
        trails : 6
    },
    addCertification : function({month, year}) {
        console.log(month,year);
    }
}

// Destructuring Object 
const { name, address, certifications} = trailblazer;
console.log(name, address, certifications);

// Destructuring Object by renaming the properties
const {name: personName, address: currentAddress, certifications: sfdcCertifications} = trailblazer;
console.log(personName, currentAddress, sfdcCertifications);

// Destructuring Object by assigning default value and renaming properties
const {
    connections = {},
    trailhead: myTrailhead = {}
} = trailblazer;
console.log(connections,myTrailhead);

// Mutating the variables
let num1 = 100, num2 = 200;
const obj = {num1: 10, num2: 20};
({num1, num2} = obj);
console.log(num1, num2);

// Nested objects 
const {trailhead: {badges,points}} = trailblazer;
console.log(badges,points);

// Calling function with destructured input 
const certification = {
    month: 'July',
    year: 2021
};
trailblazer.addCertification(certification);

No comments:

Post a Comment

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC)

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC) Introduction: Salesforce Lightning Web ...