Destructuring of es6 but passing dynamic variable

前端 未结 1 1986
死守一世寂寞
死守一世寂寞 2020-11-30 11:52

Says my state is like this:

{
  item:{
      a:\'a\',
      b:\'b\'
  }
}

Then I\'m able to pull a from item by d

相关标签:
1条回答
  • 2020-11-30 12:10

    As 4castle pointet out, you could use Computed object property names and destructuring with an additional key/value pair variables for destructuring.

    var object = { item: { a: 'a0', b: 'b0' } },
        key = 'b',
        value;
    
    ({ [key]: value } = object.item);
    
    console.log(value);

    0 讨论(0)
提交回复
热议问题