Class for Switch statements This is more to get clarity on an implementation that I did. I am working on a React, Node, and Electron application that essentially has a form that a user inputs values that will update some content files.

The application has to scale for a lot of different scenarios so I found that I kept writing switch statements to incorporate all of these scenarios. So for the sake of time I wrote a class that:


Takes in two arrays, An array of keys and array of functions that correspond to those keys.
The constructor function creates a table for the values
Then there is a method on the class that allows you to look up that value and returns it


here is the code

Array.prototype.createObjectFromKeysandValues = function(array) {
    let table = {}
    for(let pointer=0;pointer&lt;this.length;pointer++){
        let key = this[pointer]
        let value = array[pointer]
        table[key]=value
    }
    return table
}


  module.exports = class KeyFunctionMappingTable {
      constructor(keys, values){
        this.lookUpTable = Object.assign({},
         keys.createObjectFromKeysandValues(values),
         { default: () =&gt;  new Error("Value Not Found")})
         }

     findHandlerFunction(value){
        return this.lookUpTable[value]|| this.lookUpTable.default
     }
   }


So when the code is implemented it looks like this ( this scenario is for React Components) :

const factory = new KeyFunctionMappingTable(['select', 'input'], [
    (options, labelText) =&gt; ( &lt;Select options={options} labelText={labelText} /&gt;),
    (options, labelText) =&gt; ( &lt;Input labelText={labelText} /&gt;)])


I am finding that I have about three of these "mappings" throughout the code.

What I want to know is if this seems like a readable implementation and does this make sense to strangers. If you dont think so please let me know your suggestion.
You have just added an unneeded layer of complexity to the problem.

You can just create the factory as an Object

const factory = {
    select(opts, text) { return (&lt;select options={opts} labelText={text} /&gt;) },
    input(opts, text) { return (&lt;input labelText={text} /&gt;) },
    default() { return new Error("Value Not Found") }
};


Then call the function with

(factory[value] || factory.default)(opts, text);