Click here to Skip to main content
11,576,993 members (58,873 online)
Rate this: bad
Please Sign up or sign in to vote.

I have this code:
        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'; }

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 5:46am
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.

1 solution

Rate this: bad
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.


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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 221
1 DamithSL 155
2 Abhinav S 130
3 OriginalGriff 95
4 Lucifier Rocks 85
0 OriginalGriff 715
1 Sergey Alexandrovich Kryukov 631
2 Abhinav S 528
3 F-ES Sitecore 420
4 Suvendu Shekhar Giri 365

Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2015
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