Carlos Herrera Plata
Carlos Herrera Plata

Reputation: 993

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.

Upvotes: 22

Views: 10586

Answers (1)

Andy
Andy

Reputation: 63524

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);

Upvotes: 38

Related Questions