Object Destructuring (JavaScript)
image author: https://www.google.com/imgres?imgurl=https%3A%2F%2Fdmitripavlutin.com%2Fstatic%2F966c49cc1140fcd38fdfefcf42df2215%2F05127%2Fcover.png&imgrefurl=https%3A%2F%2Fdmitripavlutin.com%2Fjavascript-object-destructuring%2F&tbnid=rXwAZ6X17xjm9M&v

Object Destructuring (JavaScript)

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



要查看或添加评论,请登录

Bernardo Aguayo Ortega的更多文章

  • Get Subsets of arrays(4)

    Get Subsets of arrays(4)

    What if we only want a subset of data, no to transform the array we only want the elements that we are interesting on…

  • Perform Actions on all elements arrays(3)

    Perform Actions on all elements arrays(3)

    There are a method incredible powerful call map that allow us to make transform in each element of the array. //we have…

  • Check Element Existence in Arrays (2)

    Check Element Existence in Arrays (2)

    If you work with a hard code values is easy to work with them because you know where are the values inside the array…

  • Build Flexible Collections with Arrays (1)

    Build Flexible Collections with Arrays (1)

    There are a data structure to save collection of data call it array use [] brackets instead {}, Arrays are a kind of…

  • Merge Objects with Object Spread

    Merge Objects with Object Spread

    If you want to merge various objects you need to spread object's properties in new object and literary since ECMAScript…

  • Use Objects for Managing key-value pairs (js)

    Use Objects for Managing key-value pairs (js)

    We already know the different types of variables in JavaScript so you can see a variable as a box and an object like a…

    2 条评论
  • Shorter functions with arrow functions

    Shorter functions with arrow functions

    With ES6 we have a new form to write functions, that allow us to write shorter functions, we don't need any more the…

    4 条评论
  • How Functions Should Be Named

    How Functions Should Be Named

    A few things to remember when naming functions:Name functions after a verb which clearly conveys what they do Omit…

  • single responsibility functions

    single responsibility functions

    How to make your function only have one responsibility? As the name say it this principle talks about make functions…

  • Better functions with default parameters (javascript)

    Better functions with default parameters (javascript)

    Imagine the you have a function function convertTemperature(celsius , decimalPlaces) { ? const fahrenheit = celsius *…

    4 条评论

社区洞察

其他会员也浏览了