1

I want to initialize a array of functions dynamically,

Example :

Transform the following code :

$filter('orderBy')(datas, 
    [
        function (obj) {
             return filterTable(obj, sortInfos.fields[0]);
        },
        function (obj) {
             return filterTable(obj, sortInfos.fields[1]);
        },
        function (obj) {
             return filterTable(obj, sortInfos.fields[2]);
        },
        function (obj) {
             return filterTable(obj, sortInfos.fields[3]);
        }
    ], 
    revert);

To :

$filter('orderBy')(datas, myArrayOfFunction, revert);

3
  • 2
    Don't repeat yourself Commented Sep 18, 2014 at 7:53
  • What exactly is your question? Commented Sep 18, 2014 at 8:01
  • I have edited my question, I hope that it is clear Commented Sep 18, 2014 at 8:12

1 Answer 1

1
function createMyFuntion(sortInfoIndex) {
    return function(obj) { return filterTable(obj, sortInfos.fields[sortInfoIndex]); };
}

var myArrayOfFunction = [];
for (var i = 0; i < 4; i++) {
    myArrayOfFunction.push(createMyFuntion(i))
}

$filter('orderBy')(datas, myArrayOfFunction, revert);
Sign up to request clarification or add additional context in comments.

4 Comments

But how can I pass the "obj" to createMyFuntion ?
But how can I pass the "obj" to createMyFuntion ? - obj is an argument of a function. So these functions should be called like this: myArrayOfFunction[1](myObj) in $filter('orderBy').
All interfaces are still same. I have only wrapped your copy-pasted functions in array to createMyFuntion and for loop.
I could not do it :( ... can you write all the solution please ?

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.