javascript - Alias ES6 destructuration -


i'm creating generic function checkfield forms, , i'd able extract state (or javascript object) variable i'm passing parameter

checkfield(fieldname) {    const {      validityfieldobj,      fieldname    } = this.state     // equivalent `this.state[fieldname]`    // can't fieldname this.state destructuration,    // as been delcared in parameter.    // alternative issue? } 

to field name need use destructuring computed property name, , assign result variable:

const state = {    validityfieldobj: {},    abc: 3  };    function checkfield(fieldname) {     const {       validityfieldobj,       [fieldname]: value     } = state;      console.log(value);  }    checkfield('abc');

if need extract property name fieldname can use alias:

const state = {    validityfieldobj: {},    fieldname: 'abc'  };    function checkfield(fieldname) {    const {      validityfieldobj,      fieldname: nameoffield    } = state;      console.log(nameoffield);  }    checkfield('abc');


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -