简体   繁体   中英

Object Destructuring ignoring values

I'm trying to destructure and ignore few values, how do I do that?

For example:

const test = { name: 'John', age: 29, gender: 'male'}
function getData(...args) {
  const {,,gender} = args[0];
  console.log(gender); // should print male.
}

getData(test);

I want to ignore (not declare variables for) name and age parameters (so that my ESLint does not throw an error) at the same time use ES6.

The syntax , does not seem to work either. Any other workarounds for this problem?

You have a single arg (the object), and you should object destructuring to get gender :

 const test = { name: 'John', age: 29, gender: 'male'} function getData(arg) { const { gender} = arg; console.log(gender); // should print male. } getData(test);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM