Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I have this code:
JavaScript
myfunction=function(params){
    $.each(params,function(key,value){
        var myfun=value["formatter"];
        document.write(value["name"] + myfun());
    })
}


JavaScript
var columns= [
        {
            name: "col1 ",
            formatter: function () { return 'a'; }
        },
        {
            name: "col2 ",
            formatter: function () { return 'b'; }
        },
        {
            name: "col3 ",
            formatter: function () { return 'c'; }
        },
    ]
myfunction(columns);


My code seem to be good, but the problem is that to each column, only the function of the first element is executed to all elements. when should be executed the function according to the element.

my result:
col1 a
col2 a
col3 a


the correct result should be this
col1 a
col2 b
col3 c
Posted
Comments
Sergey Alexandrovich Kryukov 16-Apr-13 12:41pm    
I see. This is trivial, but the problem is: you never show the code where you are trying to call formatter functions stored in the array. Besides, the idea is bad. Rather, you should use associated array so the string is used as a key.
—SA

1 solution

Please see my comment to the question. Do something like:
JavaScript
colums[0] = function () { return 'a'; }
colums[1] = function () { return 'b'; }
colums[2] = function () { return 'c'; }
//...

// the calls would look like
value = columns[someIntegerIndex]();


I hope this is just for research, and the functions are supposed to be less trivial, because if you really want to return a character by a function without parameters, you would better simply have an array of characters.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900