I'm using TypeScript 2.2.1 with target ES5
I have couple of enums like below
enum PermissionLevel {
ACCOUNT_OWN,
ACCOUNT_OTHER,
USER_OWN
}
enum Permission {
ACCOUNT_CREATE,
USER_CREATE
}
I have a input JSON as below
{
"ACCOUNT_OWN": [
"USER_CREATE",
"ACCOUNT_CREATE"
],
"ACCOUNT_OTHER": [
"USER_CREATE"
]
}
I want to store this input as Map<Permission, Array<PermissionLevel>>
Following is my code
// assume authJson is the input and is a valid JSON.
this.userPermissions = new Map<Permission, Array<PermissionLevel>>();
for (let [level, permissions] of Object.entries(authJson)) {
let newLevel: PermissionLevel = <any>PermissionLevel[level];
for (let perm of permissions) {
let newPermission: Permission = <any>Permission[perm];
if (this.userPermissions.get(newPermission)) {
this.userPermissions.set(newPermission, this.userPermissions.get(newPermission).concat(newLevel));
} else {
this.userPermissions.set(newPermission, new Array<PermissionLevel>().concat(newLevel));
}
}
}
console.log(JSON.stringify(this.userPermissions));
The output of the last statement is empty :(. Not sure what I'm doing wrong here.
Update: I just found there are run time errors line below
EXCEPTION: Uncaught (in promise): ReferenceError: PermissionLevel is not defined
Here is a TS fiddle - https://jsfiddle.net/hrwyc6zv/1/
console.log(this.userPermissions);, it prints 0's and 1's. I guess they are indexes. Sounds like the map is populated correctly with the Enums... Is that correct ?