Object Destructuring (JavaScript)
Bernardo Aguayo Ortega
Senior Software Engineer @ ZeroCopyLabs | React & Next.js Specialist | Building Scalable Web Applications with JavaScript, TypeScript, and Node.js
Object destructuring allows us to pull properties from an object and make them into variables, in other words allow us to use only the properties that we want to use from any object, that is usefully because an object can have many properties that many times we don't use and with that we avoid loose data.
how can we destructuring an object ?
//you have this object cost colors = { blue: '#f00', yellow: '#0f0', red: '00f' pink: { lightPink: '#000' } } // instead access object properties through dot notation like the example below colors.blue colors.red // you can access with destructuring const { blue, red, yellow, pink: { lightPink } } = colors
remember Object destructuring allows us to pull properties from an object and make them into variables, that makes more readable our code and concise.
And how to destructuring an object as function parameter ?
// it's exactly the same, only instead of the file body, you put it in the parameters of a function
const user = { name: 'Bernardo', lastName: 'Aguayo' } //function function sayHey ({name}){ console.log(name) } //call the function with the object seyhey(user)
I hope that you enjoy this lecture,with love Bernardo <3