21

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.

1 Answers1

37

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);
Andy
  • 61,948
  • 13
  • 68
  • 95