从同一对象内的另一个对象分配值

时间:2018-08-08 10:31:24

标签: javascript json

如何在声明时从同一JSON获取另一个对象的值?

var constant = {

    roles: {
      ADMIN: 'admin',
      CONSUMER: 'consumer',
      SUPER_ADMIN: "super_admin"
    },

    webAccess : [roles.ADMIN]

}

console.log(constant);

预期输出:

{
  "roles": {
    "ADMIN": "admin",
    "CONSUMER": "consumer",
    "SUPER_ADMIN": "super_admin"
  },
  "webAccess": [
    "admin"
  ]
}

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

var constant = {

    roles: {
      ADMIN: 'admin',
      CONSUMER: 'consumer',
      SUPER_ADMIN: "super_admin"
    },

    get webAccess() {
        return [this.roles.ADMIN];
    } 

}

console.log(constant);