Associative Array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ravi

    Associative Array?

    Hi All:
    I want to write a script to do name mapping. The argument "internal" is
    the name to be mapped and the return value is the mapped name i.e. if
    "apple" is the input "Xapple" is the output. However the argument may
    not have an entry in the associative array. for e.g. if the argument is
    "strawberry " the output should be "strawberry " as there is no mapping
    for "strawberry ". I wrote the following script but it does not work.


    function GetExternal(int ernal)
    {
    var my_cars= new Array()
    my_cars["apple"]="Xapple";
    my_cars["peach"]="Xpeach";
    my_cars["orange"]="Xorange";
    my_cars["banana"]="Xbanana";
    my_cars["plum"]="Xplum";

    if(my_cars[internal]!="")
    return my_cars[internal];
    else
    return internal;
    }


    Can someone kindly point out what might be wrong?

    TIA.

  • Martin Honnen

    #2
    Re: Associative Array?



    Ravi wrote:[color=blue]
    > I want to write a script to do name mapping. The argument "internal" is
    > the name to be mapped and the return value is the mapped name i.e. if
    > "apple" is the input "Xapple" is the output. However the argument may
    > not have an entry in the associative array. for e.g. if the argument is
    > "strawberry " the output should be "strawberry " as there is no mapping
    > for "strawberry ". I wrote the following script but it does not work.
    >
    >
    > function GetExternal(int ernal)
    > {
    > var my_cars= new Array()[/color]

    I guess
    var my_cars = new Object();
    is more appropriate.[color=blue]
    > my_cars["apple"]="Xapple";
    > my_cars["peach"]="Xpeach";
    > my_cars["orange"]="Xorange";
    > my_cars["banana"]="Xbanana";
    > my_cars["plum"]="Xplum";
    >
    > if(my_cars[internal]!="")
    > return my_cars[internal];
    > else
    > return internal;
    > }[/color]

    if (typeof my_cars[internal] != 'undefined')
    return my_cars[internal];
    else
    return internal;

    --

    Martin Honnen


    Comment

    • Douglas Crockford

      #3
      Re: Associative Array?


      "Ravi" <rg27@cse.buffa lo.edu> wrote in message
      news:bp2s7r$eon $1@prometheus.a csu.buffalo.edu ...[color=blue]
      > Hi All:
      > I want to write a script to do name mapping. The argument "internal" is
      > the name to be mapped and the return value is the mapped name i.e. if
      > "apple" is the input "Xapple" is the output. However the argument may
      > not have an entry in the associative array. for e.g. if the argument is
      > "strawberry " the output should be "strawberry " as there is no mapping
      > for "strawberry ". I wrote the following script but it does not work.
      >
      >
      > function GetExternal(int ernal)
      > {
      > var my_cars= new Array()
      > my_cars["apple"]="Xapple";
      > my_cars["peach"]="Xpeach";
      > my_cars["orange"]="Xorange";
      > my_cars["banana"]="Xbanana";
      > my_cars["plum"]="Xplum";
      >
      > if(my_cars[internal]!="")
      > return my_cars[internal];
      > else
      > return internal;
      > }[/color]

      In JavaScript, the structure you want is Object, not array. Get used to the
      Literal Object Notation.

      var getExternal = function (internal) {
      return getExternal.dat a[internal] || internal;
      }
      getExternal.dat a = {
      apple: "Xapple",
      peach: "Xpeach",
      orange: "Xorange",
      banana: "Xbanana",
      plum: "Xplum"};



      Comment

      Working...