In Javascript, how to apply a dynamically created function name -


my goal efficiently apply dynamically chosen set of transforms each element of matrix. store selected functions in array, apply each of them in single pass thru matrix.

my question is, how can dynamically create name of function add array of functions?

this fiddle contains attempt. question included in comment block.

function dynam () {    var prepend = 'before - ';   var append = ' - after';   var whichcase = 'upper';    var functionstoapply = [];   var matrix = [                    ['abc', 'def'],                    ['ghi', 'jkl'],                ];    var out = 'initial: ' + matrix.join(' | ');   document.getelementbyid('output').innerhtml =  out + '\n';   console.log(out);    // set transforms   if (whichcase == 'lower') {     functionstoapply.push(function(v) {return v.tolowercase();});   } else if (whichcase == 'upper'){     functionstoapply.push(function(v) {return v.touppercase();});   }  // how can function defined dynamically? // perhaps like: //  if(['lower','upper'].indexof(whichcase) != -1) { //    functionstoapply.push(function(v) {'return v.to' + + 'case();'}); //  }    if (prepend && prepend.length > 0 && prepend != 'none') {     functionstoapply.push(function(v) {return prepend + v;});   }   if (append && append.length > 0 && append != 'none') {     functionstoapply.push(function(v) {return v + append;});   }  // apply of transforms each of elements of matrix   matrix = matrix.map(function(row){     return row.map(function(val) {       (var fn = 0; fn < functionstoapply.length; fn++) {         val = functionstoapply[fn](val);         }       return val;     })   });     out = 'final: ' + matrix.join(' | ');   document.getelementbyid('output').innerhtml +=  out + '\n';   console.log(out); } 

i declare functions in hash in case, can call them based on value passed in, key hash.

updated: i've added way lower/upper function dynamically, , call it.

function dynam(whichcase, prepend, append, matrix) {     var functionhash = {    "lower" : function(v) {return v.tolowercase();},    "upper" : function(v) {return v.touppercase();},    "prepend" : function(v) {return prepend + v;},    "append": function(v) {return v + append;}   }    // case function based on word passed in,   // can way.     var lowerupperfunction = string.prototype['to' + whichcase + 'case'];   var str = lowerupperfunction.call("xyz");   console.log(str);      var functionstoapply = [];        var out = 'initial: ' + matrix.join(' | ');    console.log(out);      // see how take whichcase , make call hash?   functionstoapply.push(functionhash[whichcase]);    if (prepend && prepend.length > 0 && prepend != 'none') {      functionstoapply.push(functionhash["prepend"]);    }    if (append && append.length > 0 && append != 'none') {      functionstoapply.push(functionhash["append"]);  }    // apply of transforms each of elements of matrix    matrix = matrix.map(function(row){      return row.map(function(val) {        (var fn = 0; fn < functionstoapply.length; fn++) {          console.log("applying function val" + val );          val = functionstoapply[fn](val);          }        return val;      })    });       out = 'final: ' + matrix.join(' | ');    return out;  }        var p = 'before - ';    var = ' - after';    var w = 'upper';    var m = [               ['abc', 'def'],               ['ghi', 'jkl'],           ];       console.log( dynam(w, p, a, m) );


Comments