我可以解构一个 属性 名称由变量本身定义的变量吗?

Can I destructure a variable where property name defined by a variable itself?

例如,我在 localStorage 中有对象属性 Meteor.userIdMeteor.loginToken。我可以按如下方式解构它们:

const {
      'Meteor.userId': userId,
      'Meteor.loginToken': loginToken,
    } = localStorage;

但是我可以将Meteor.userIdMeteor.loginToken定义为变量吗?例如:

const METEOR_USER_ID = 'Meteor.userId';
const METEOR_LOGIN_TOKEN = 'Meteor.loginToken';

尝试使用以下代码,但不起作用:

const {
      METEOR_USER_ID: userId,
      METEOR_LOGIN_TOKEN: loginToken,
    } = localStorage;

你需要一个computed property来获取变量的键。

const {
    [METEOR_USER_ID]: userId,
    [METEOR_LOGIN_TOKEN]: loginToken,
} = localStorage;