Associative array key

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crazytegger
    New Member
    • May 2006
    • 22

    Associative array key

    I need help, im sure the solution is simple, but it's driving me nuts. Im trying to retrieve the value of js_array['pearl']['a'] using js_array['pearl'][selectedObj.sel ectedIndex]; where selectedObj.sel ectedIndex is a string value from a select box. I need to wrap quotes around selectedObj.sel ectedIndex for it to retrieve the associative key properly, but everything ive tried has failed. This works fine if the key is numeric, but fails if the key is a string. I'm sorry if I didn't explain this very well, my brain is kind of fried today. Can someone please help me???


    Code:
     
    <script language='JavaScript'> 
    var js_array = new Array();
    js_array['stain'] = new Array();
     
     
    js_array["pearl"]["a"] = "pearla.jpg";
     
     
    function pearlchange(selectedObj){
    document.pearlimg.src=imgDir + js_array['pearl'][selectedObj.selectedIndex];
    }
    </script>
     
    <form>
    <select name="pearl" onchange='pearlchange(this)' >
    <option value='a'>Violet (a)</option>
     
    </select>
    </form>
     
     
    <img name='pearlimg' src="" />
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Use selectedObj.val ue in place of selectedIndex.

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #3
      just a note:

      JavaScript has no associative Arrays ... even when you do what you did. In fact you extend the native JavaScript array with new properties ... and this is a 'misuse' of the array as an object ... so the correct way would be to use an object at all. the misuse could lead to strange behaviour when you use for-in-loops or rely on array-methods or properties ... for example length will be zero.

      so the correct way would be ... the alert shows the construct in FF:

      [CODE=javascript]var foo = {};
      var bar = {};

      bar.key1 = 'foobar';

      foo.foo_key1 = bar;

      alert(foo.toSou rce());[/CODE]
      kind regards

      Comment

      • crazytegger
        New Member
        • May 2006
        • 22

        #4
        Thanks for the help, much appreciated.

        Justin

        Comment

        • crazytegger
          New Member
          • May 2006
          • 22

          #5
          I have another problem now. FF doesn't like it when I assign a numeric "key" to my object. What am I doing wrong here?

          Code:
          var stain = {};
          var pearl = {};
          stain.1 = 'stain1.jpg';  //This produces a 'missing ; before statement' error
          pearl.a = 'pearla.jpg'; //This works fine

          I'm also having problem with using a variable as my object "key" value:

          Code:
           
          var optionImg = {};
          var pearl = {};
          pearl.a = 'pearla.jpg';
          pearl.b = 'pearlb.jpg';
          pearl.c = 'pearlc.jpg';
          pearl.e = 'pearle.jpg';
          pearl.f = 'pearlf.jpg';
          pearl.g = 'pearlg.jpg';
          pearl.h = 'pearlh.jpg';
          optionImg.pearl = pearl;
          alert(optionImg.toSource());
          var imgDir='http://vikingcue.com/images/options/';
          
           
          function pearlChange(selectedObj){
          var testpath=imgDir + optionImg.pearl.selectedObj;
          alert(selectedObj in optionImg.pearl.toSource());
          alert(testpath.toSource());
          }
          I need the selectObj variable to reference the key, instead I get the error: optionImg.pearl .selectedObj is undefined. I'm sorry if these questions are elementary, I'm just trying to learn a few things here.


          Here is the select box:

          <select onChange='pearl Change(this.val ue)>
          <option value='a'>Pearl A</option>
          </select>

          Thanks,

          Justin

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Use square bracket notation:
            Code:
            optionImg.pearl[selectedObj]

            Comment

            • crazytegger
              New Member
              • May 2006
              • 22

              #7
              Thanks, that fixed both problems I was having.

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                You're welcome. Glad it did!

                Comment

                Working...