Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 229
Carlos Herrera Plata Avatar asked Sep 08 '17 17:09

Carlos Herrera Plata


Video Answer


1 Answers

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);
like image 144
Andy Avatar answered Sep 29 '22 07:09

Andy