Click here to Skip to main content
13,046,184 members (84,451 online)
Rate this:
Please Sign up or sign in to vote.
See more:

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 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2017
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