naming array values?

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

    naming array values?

    Hi, this is an array that is used for a dropdown menu.

    var imageArray = new Array(
    "ecwp://" + document.locati on.host + "/massey/images/massey/
    massey_07_fullr es.ecw",
    "ecwp://" + document.locati on.host + "/sampleiws/images/usa/
    1metercalif.ecw ",
    "ecwp://" + document.locati on.host + "/sampleiws/images/australia/
    parramatta.ecw" ;

    when the menu is displayed it shows the whole value

    "ecwp://" + document.locati on.host + "/massey/images/massey/
    massey_07_fullr es.ecw""

    is there a way i can just display a name for the image in the dropdown
    menu, like "Massey 2007"

    cheers
  • Tim Williams

    #2
    Re: naming array values?


    "Pukeko" <pukeko.taniwha @gmail.comwrote in message
    news:eab0f478-cfc2-483f-b888-e095d4c2dbee@i3 6g2000prf.googl egroups.com...
    Hi, this is an array that is used for a dropdown menu.
    >
    var imageArray = new Array(
    "ecwp://" + document.locati on.host + "/massey/images/massey/
    massey_07_fullr es.ecw",
    "ecwp://" + document.locati on.host + "/sampleiws/images/usa/
    1metercalif.ecw ",
    "ecwp://" + document.locati on.host + "/sampleiws/images/australia/
    parramatta.ecw" ;
    >
    when the menu is displayed it shows the whole value
    >
    "ecwp://" + document.locati on.host + "/massey/images/massey/
    massey_07_fullr es.ecw""
    >
    is there a way i can just display a name for the image in the dropdown
    menu, like "Massey 2007"
    >
    cheers
    How are you building the menu ? If you have code to do that (and it's
    always useful to include that in your post), then just adjust to suit.

    Otherwise, you haven't provided enough information to answer your queston.

    Tim


    Comment

    • Tom Cole

      #3
      Re: naming array values?

      On May 5, 2:24 am, "Tim Williams" <timjwilliams at gmail dot com>
      wrote:
      "Pukeko" <pukeko.tani... @gmail.comwrote in message
      >
      news:eab0f478-cfc2-483f-b888-e095d4c2dbee@i3 6g2000prf.googl egroups.com...
      >
      >
      >
      >
      >
      Hi, this is an array that is used for a dropdown menu.
      >
      var imageArray = new Array(
      "ecwp://" + document.locati on.host + "/massey/images/massey/
      massey_07_fullr es.ecw",
      "ecwp://" + document.locati on.host + "/sampleiws/images/usa/
      1metercalif.ecw ",
      "ecwp://" + document.locati on.host + "/sampleiws/images/australia/
      parramatta.ecw" ;
      >
      when the menu is displayed it shows the whole value
      >
      "ecwp://" + document.locati on.host + "/massey/images/massey/
      massey_07_fullr es.ecw""
      >
      is there a way i can just display a name for the image in the dropdown
      menu, like "Massey 2007"
      >
      cheers
      >
      How are you building the menu ?  If you have code to do that (and it's
      always useful to include that in your post), then just adjust to suit.
      >
      Otherwise, you haven't provided enough information to answer your queston.
      >
      Tim- Hide quoted text -
      >
      - Show quoted text -
      Javascript arrays are a little tricky in this regards. Yes you can
      "name" and Array entry, or more accurately assign a "name" as the key.
      The problem is, in my experience, that when store array elements in
      this manner, the length of the array is not adjusted. For example this
      is totally legit:

      var images = new Array();
      images['Massey 2007'] = "ecwp://" + document.locati on.host + "/massey/
      images/massey/massey_07_fullr es.ecw";
      images['Massey 2006'] = "ecwp://" + document.locati on.host + "/massey/
      images/massey/massey_06_fullr es.ecw";

      However if you call alert(images.le ngth) you will get 0. Of course
      even if it didn't, this wouldn't help, because you need the key (or
      "name") not just the value. And there is no method that I have found
      to retrieve the list of keys an array is using. So...the problem isn't
      "naming" your array elements, but how do you remember that list of
      names so you can later retrieve the values.

      You will probably need two arrays, one to store the "names" and the
      other to store the "values". How you arrange to coordinate the two are
      up to you. You could simply ensure that both arrays are the same
      length and that the element 0 in the names array correlates to the
      element 0 in the values array, or you could keep the names in array 1
      and use the names as the keys in array 2. For example:

      var names = new Array();
      var urls = new Array();

      names.push("Mas sey 2007");
      urls["Massey 2007"] = "ecwp://" + document.locati on.host + "/massey/
      images/massey/massey_07_fullr es.ecw";

      Then you could loop through the names array and pull the url using
      that value as the key from the second array. For example:

      for (var i = 0; i < names.length; i++) {
      var name = names[i];
      var url = urls[name];
      //build your Option here...

      }

      HTH...

      Comment

      • =?ISO-8859-1?Q?Une_B=E9v?==?ISO-8859-1?Q?ue?=

        #4
        Re: naming array values?

        Tom Cole <tcole6@gmail.c omwrote:
        >
        var names = new Array();
        var urls = new Array();
        it is easier to use an object :

        var myObject={};
        myObject[name_1]=url_1;
        ....
        myObject[name_n]=url_n;


        that way, unless u've added properties to Object, using
        Object.prototyp e, u can get all the key/value pair by

        for(var key in myObject{
        alert("key = "+key+", value = "+value);
        }
        --
        Une Bévue

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: naming array values?

          Pukeko wrote:
          Hi, this is an array that is used for a dropdown menu.
          From your question below I surmise you are probably talking about a `select'
          element that only works with present and enabled client-side script support,
          which would be a really bad idea.
          var imageArray = new Array(
          "ecwp://" + document.locati on.host + "/massey/images/massey/
          massey_07_fullr es.ecw",
          document.locati on has been deprecated more than 10 years ago.
          "ecwp://" + document.locati on.host + "/sampleiws/images/usa/
          1metercalif.ecw ",
          "ecwp://" + document.locati on.host + "/sampleiws/images/australia/
          parramatta.ecw" ;
          Your source code is syntactically incorrect: the closing `)' for the
          constructor call is missing. That aside, I would do at least:

          var imageArray = new Array(
          new Array("ecwp://", "/massey/images/massey/massey_07_fullr es.ecw"),
          new Array("ecwp://", "/sampleiws/images/usa/1metercalif.ecw "),
          new Array("ecwp://", "/sampleiws/images/australia/parramatta.ecw" )
          );

          for (var i = 0, len = imageArray.leng th; i < len; i++)
          {
          imageArray[i] = imageArray[i].join(document. location.host);
          }
          when the menu is displayed it shows the whole value
          >
          "ecwp://" + document.locati on.host + "/massey/images/massey/
          massey_07_fullr es.ecw""
          >
          is there a way i can just display a name for the image in the dropdown
          menu, like "Massey 2007"
          Yes, there is, for example by using your own constructor:

          /**
          * Constructs a new <code>Item</codeobject.
          *
          * @param uriParts: Array
          * 2-element array containing the scheme and the path of
          * the target URI. The parts are joined with the host name
          * of the URL of the current document.
          * @param text: optional string
          * Text for the menu item; the default is the target URI.
          * @constructor
          */
          function Item(uriParts, text)
          {
          /**
          * URI of the target resource
          */
          this.uri = uriParts.join(w indow.location. host);

          /**
          * Text for the menu item
          */
          this.text = text || this.uri;
          }

          var imageArray = new Array(
          new Item(
          new Array("ecwp://", "/massey/images/massey/massey_07_fullr es.ecw"),
          "Massey 2007"
          ),
          ...
          );

          You should then get informed how HTML `select' and `option' elements work,
          or RTFM of your "menu" script of which you have not even posted the relevant
          parts here.


          PointedEars
          --
          realism: HTML 4.01 Strict
          evangelism: XHTML 1.0 Strict
          madness: XHTML 1.1 as application/xhtml+xml
          -- Bjoern Hoehrmann

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: naming array values?

            Tom Cole wrote:
            Javascript arrays are a little tricky in this regards. Yes you can
            "name" and Array entry, or more accurately assign a "name" as the key.
            The problem is, in my experience, that when store array elements in
            this manner, the length of the array is not adjusted.
            Because you are not storing array elements then, but augment the Array object.
            For example this is totally legit:
            >
            var images = new Array();
            images['Massey 2007'] = "ecwp://" + document.locati on.host + "/massey/
            images/massey/massey_07_fullr es.ecw";
            images['Massey 2006'] = "ecwp://" + document.locati on.host + "/massey/
            images/massey/massey_06_fullr es.ecw";
            And why not? Array objects are native objects and, some properties with the
            ReadOnly attribute aside, native objects can be augmented with any number of
            properties.
            However if you call alert(images.le ngth) you will get 0.
            It helps to use a collection implementation instead.
            Of course even if it didn't, this wouldn't help, because you need the key (or
            "name") not just the value. And there is no method that I have found
            to retrieve the list of keys an array is using.
            var a = ["x", 42];
            a["foo"] = "bar";

            var out = [];

            for (var key in a)
            {
            var p = a[key];
            var t = typeof p;
            if (t == "string")
            {
            p = '"' + p.replace(/"/g, "\\$&") + '"';
            }

            out.push(key + ": " + t + " = " + p);
            }

            // displays the enumerable properties of `a'
            // and the objects in its prototype chain
            window.alert(ou t.join("\n"));

            Note that this can be useful with a collection implementation; if a
            collection is not required, then one should use an Object object
            instead of an Array object.
            So...the problem isn't "naming" your array elements, but how do you
            remember that list of names so you can later retrieve the values.
            Provided one does not augment Object.prototyp e or Array.prototype , it is not
            as hard to implement as you think.
            You will probably need two arrays, one to store the "names" and the
            other to store the "values".
            No, this is not necessary. You can create user-defined objects that are
            elements of the array instead.


            PointedEars
            --
            realism: HTML 4.01 Strict
            evangelism: XHTML 1.0 Strict
            madness: XHTML 1.1 as application/xhtml+xml
            -- Bjoern Hoehrmann

            Comment

            Working...