1

Got an array with functions. I want to do a function that returns a function from the array with function name given as argument.

var arr = [
  function Dog(){},
  function Cat(){}
];



var getFunction = function(name){
    return // should return the function with matching name
};

var dogFunction = getFunction('Dog'); // returns dog function.

https://jsfiddle.net/zcjd9pyz/

Is this possible?

0

5 Answers 5

3

if you do an associative array, it is possible

var arr = {
    'dog' : function Dog(){},
    'cat' : function Cat(){}
};

arr['dog']();
Sign up to request clarification or add additional context in comments.

4 Comments

And to get the function: var myFcuntion = arr['functionName'];
@Logar314159 I don't get your comment
@DeblatonJean-Philippe I think Logar314159 meant something like: arr.getFunction = function(name) { return this[name]; } ... this would give the syntax requested in the question. Not sure it's necessary, but if so...
@DeblatonJean-Philippe. I Already edited the comment, it was unclear, thank you.
2

Functions have a name property:

var getFunction = function(name){
    for (var i=0; i<arr.length; i++) {
       if (arr[i].name===name) return arr[i];
    }
    return // return undefined
};

If you want to have a fast access, you can precompute a map by first iterating:

var map = arr.reduce(function(m,f){ m[f.name]=f; return m}, {});

which allows

var fun = map["Dog"];

Computing the map in code instead of typing it yourself lets you not repeat the name. A DRY code is easier to maintain.

EDIT: I'm not sure functions have a name on IE but I can't test it.

6 Comments

Very huge amounts I'm working with. Is this or doing it as an associative array fastest?
@PerStröm An associative array is faster but you should check it really matters
According to Mozilla.org, the name function property is not supported in Internet Explorer. You could polyfill it by extracting the function's name from the value returned by toString()
What is an associative array? I thought that was something they had in PHP.
@torazaburo That's another name for what you probably know as a map. Any object is (about) an "associative array" in JS, meaning you can set and get properties by name
|
1

In ES6 you could do it without modifying the array (or in all browsers except Internet Explorer if you replace the arrow function with a normal one and use a polyfill for find:

var getFunction = function(name){
    return arr.find( func => name === func.name );
};

Even in ES6 though, I don't see a good reason to do that. I think you should follow Deblaton Jean-Philippe's answer and change the array to an object, mapping the names to the functions.

1 Comment

This won't work. Either remove the curly brackets, or add a return. Also, you're mixing up el and func. MIssed morning coffee?
0

You can use this sample work around of mine, instead of matching for string you can use it based on function name

https://gist.github.com/freewayz/56bd9db6d4164a42be75

var myArray = [{"name" : "pitaside", "id" : 1}, {"name":"github", "id" : 3}]
filterArrayByType: function (arrayToMatch, fieldType, matcher) {
        if(! arrayToMatch instanceof Array){throw ("Not an Array")}
        var filterTypeToReturn = arrayToMatch.filter((items) => {
                var temp;
                if (items[String(fieldType)] === matcher) {
                    temp = items[String(fieldType)]
                }
                return temp;
            }
        );
        return filterTypeToReturn;
}
var myMatcher = 'github'
var id3 = filterArrayByType(myArray, 'name', myMatcher)[0].id

//returns 3

Comments

-1

You can use Function.prototype.toString(). Unlike name it is supported by most of the modern browsers as well as by Node.js.

var arr = [
    function  Dog ( ) {},
    function  Cat ( ) {}
];

var getFunction = function(name){
    'use strict';
    // could use find but it isn't supported by IE
    return arr.filter(function (func) {
        return /^function\s+(\w+)/.exec(func.toString())[1] === name;
    })[0];
};

console.log(getFunction('Dog'));
console.log(getFunction('Cat'));
console.log(getFunction('Unknown'));

2 Comments

Won't work, because \w does not include dollar sign etc.
what browsers don't support it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.