LStarky LStarky - 1 year ago 170
Javascript Question

Load array of Enum values from Json file

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.


"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];
// test enumData

// lookup value of key

In my Aurelia template, I would want something like this:

<select ref="absencecode">
<option repeat.for="keyvalue of enumKeys.AbsenceCode" value="${keyvalue.key}">${keyvalue.value}</option>

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?

Answer Source

You could use a Value Converter to process object. In fact, there is a nice example for that in the [Documentation, last section].

Applying above example to your case, it's even possible to process objects without any prior transformation.

Gist demo:

I wouldn't say it’s the most optimal solution, though. You might want to transform the data somehow before passing it to repeat.for. Just showing a possibility here.


<!-- First level properties -->
<div repeat.for="mainKey of data | keys">

    <!-- Sublevel - Value Object properties -->
        <option value="">---</option>
        <option repeat.for="code of data[mainKey] | keys" 

keys value converter:

export class KeysValueConverter {
  toView(obj) {
    return Reflect.ownKeys(obj);


But how do I target one specific item without having to iterate over all of them?

I've extended the original gist demo, you can check it out.

This would work, but it wouldn't be reusable

<label>Absence Code</label>

    <option value="">---</option>
    <option repeat.for="code of data.AbsenceCode | keys" 

A better way would be to create a custom element

(Note: <require> is there for demo purposes. Normally, you'd add it to globalResources.)

Organizing above template into a custom element with source and name bindable properties:

  • source: your data object
  • name: first-level property of data object (e.g. AbsenceCode)



    <require from="./keys-value-converter"></require>

    <select name="${name}" class="form-control">
      <option value="">---</option>
      <option repeat.for="code of source[name] | keys" value="${code}">${source[name][code]}</option> 


You can also use name property in conjunction with aurelia-i18n to display a meaningful label. E.g. ${name | t}.


import {bindable} from 'aurelia-framework';

export class EnumList {

  @bindable source;
  @bindable name;



Individual dropdowns:

<enum-list source.bind="data" name="AbsenceCode"></enum-list>
<enum-list source.bind="data" name="AuthenticationLog"></enum-list>

Since <enum-list> has all the data, its name property can also be changed at runtime! :)

<select class="form-control" value.bind="selectedType"> 
  <option repeat.for="mainKey of data | keys" value="${mainKey}">${mainKey}</option> 
<enum-list source.bind="data" name.bind="selectedType"></enum-list>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download