Friday, January 6, 2023

JavaScript Destructuring Object Salesforce

 // Spread Operator - Seperate the array as multiple values

const myArray = [10, 20, 30];
const myNewArray = [0, 5, ...myArray];
console.log(myNewArray); // [ 0, 5, 10, 20, 30 ]
console.log(...myArray); // 10 20 30

const trailblazer = {
    name : 'Srinu',
    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);
    },
	addCertifications : function(cert1, cert2, cert3) {
		console.log(`New certifications: ${cert1} | ${cert3} | ${cert3}`);
	}
}

// Creating new array with additional value.
const latestCertifications = [...trailblazer.certifications, 'Salesforce CPQ Consultant'];
console.log(latestCertifications);

// Join Arrays as new array 
const info = [...trailblazer.certifications,...myArray];
console.log(info);

// Iterables: arrays, strings, maps and sets
const myName = 'Srinu';
const chars = [...myName, ' ', 'Sfdc'];
console.log(chars);

// Supply array values as seperate arguments to a function
const newCertifications = [prompt('Enter Certification1'),prompt('Enter Certification2'),prompt('Enter Certification3')];
trailblazer.addCertifications(...newCertifications);

// Spread with object 
const newtrailblazer = {...trailblazer,appExchange : 'NA'};
console.log(newtrailblazer);

No comments:

Post a Comment