Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hello,
 
I have this code:
myfunction=function(params){
    $.each(params,function(key,value){
        var myfun=value["formatter"];
        document.write(value["name"] + myfun());
    })
}
 
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 16-Apr-13 6:46am
Comments
Sergey Alexandrovich Kryukov at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please see my comment to the question. Do something like:
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
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 85
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100