Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to pass array element name?
Message
 
 
À
30/11/2015 10:58:31
Information générale
Forum:
Javascript
Catégorie:
Autre
Divers
Thread ID:
01628031
Message ID:
01628077
Vues:
25
>>>>Here is a simple hard-coded example:
>>>>
>>>>
>>>>function MyTest() {
>>>>
>>>> var MyObject = .... object is created
>>>> // It is known that the MyObject refers to an array of two columns
>>>> // To get value of the row 0 and column named "MY_COL1"
>>>> var Col1Val = MyObject[0].MY_COL1;
>>>>}
>>>>
>>>>
>>>>Now I want to pass the name 'MY_COL1' to the above function so that it can be used with various objects.
>>>>Here is what I tried (not working):
>>>>
>>>>
>>>>MyTest("MY_COL1");
>>>>
>>>>function MyTest( cCol1Name ) {
>>>>
>>>> var MyObject = .... object is created
>>>> // It is known that the MyObject refers to an array of two columns
>>>> // To get value of the row 0 and column named "MY_COL1"
>>>> var Col1Val = MyObject[0].cCol1Name;
>>>>
>>>> // Does not work!
>>>>}
>>>>
>>>>
>>>>TIA
>>>
var Col1Val = MyObject[0].[cCol1Name];
>>
>>First, thank you. But when I add the square brackets around the cCol1Name the first (left-side) bracket has a red squiggly line underneath (forgot the technical term for it) and when I hover the mouse over this red the message says "Expected Identifier".
>
>comments can be misleading: untested
>
>function MyTest( cCol1Name ) {
>var MyObject = .... object is created
>var Row = MyObject[0].cCol1Name;
>var Col1Val = Row[cCol1Name]
>// var Col1Val = eval( "Row." + cCol1Name)
>}
>>>>

Thank you.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform