I have a local JSON file containing all of my Enum key-value pairs and would like to load them into an array that I can use easily.
enum.json
{
"AbsenceCode": {
"E": "Excused",
"U": "Unexcused"
},
"ActiveInactive": {
"A": "Active",
"I": "Inactive"
},
"AuthenticationLog": {
"1": "Staff",
"2": "ParentAccess",
"3": "StudentAccess"
},
"YesNo": {
"0": "Yes",
"1": "No"
}
}
In my Javascript code, I want to load all of the key-value pairs into an array or object that allows me to easily access them, with the end goals of (a) doing value lookup and (b) creating select boxes.
I started something like this but I'm not wrapping my mind around it correctly and also somewhat unsure of whether this should be done with an array or an object, and whether JavaScript allows the type of array necessary to do this.
// load enumData
var enumKeys = $.getJSON("enum.json", function(json) {
var array = [];
for (var key in json) {
var item = json[key];
for (var keyvalue in item) {
var value = item[keyvalue];
}
array.push(parsed[key])
}
});
// test enumData
console.log(enumKeys["YesNo"]);
// lookup value of key
console.log(enumKeys["AbsenceCode"]["U"]);
In my Aurelia template, I would want something like this:
<template>
<select ref="absencecode">
<option repeat.for="keyvalue of enumKeys.AbsenceCode" value="${keyvalue.key}">${keyvalue.value}</option>
</select>
</template>
My code is "inspired" by the answers to a lot of other similar cases but I didn't find any that matched this exact scenario. Any help would be appreciated! What code should I use to load enumKeys? How do I use the loaded array/object?