Search code examples
javascriptecmascript-6destructuring

ES6 destructuring two objects with same property name


i have two javascript object with the next syntax:

let section = { name: "foo", tables: [] }
let field   = { name: "bar", properties: {} }

and a function who expect those objects, but in the function i only use the name of each object, so i wanted to know if i can destructuring the two objects in the function's declaration like:

function something( {name}, {name} ) {
  //code
} 

the first should be section.name and the second should be field.name.

Is there a way two do a destructuring in this scenario? or should i spect only the names in the function?

Which is better?

Thank you.


Solution

  • Yup, it looks like you can label/reassign the parameters: {before<colon>after}

    var section = { name: 'foo', tables: [] };
    var field = { name: "bar", properties: {} };
    
    function something({ name: sectionName }, { name: fieldName }) {
      console.log(sectionName, fieldName);
    }
    
    something(section, field);