Array Referencing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wangers16
    New Member
    • Jul 2007
    • 57

    Array Referencing

    Hi all,

    I have the following array with which I need to reference to the second item in every array and write it out onto the page, how can this be done?

    Code:
    var arr = new Array();
    arr[0] = 'images/001.jpg','Image 1 Description SECOND ITEM';
    arr[1] = 'images/002.jpg','Image 2 Description SECOND ITEM';
    Thanks in advance.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    you may use the following example ... the object in the array-elements makes it easy to refer:

    [CODE=javascript]arr[0] = {
    src: 'images/001.jpg',
    dsc: 'Image 1 Description SECOND ITEM'
    };

    // now refer with the following:

    arr[0].src // your source
    arr[0].dsc // your description
    [/CODE]
    kind regards

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Define each one like this
      Code:
      var arr = [];
      arr[0] = ['images/001.jpg','Image 1 Description SECOND ITEM'];
      arr[1] = ['images/002.jpg','Image 2 Description SECOND ITEM'];
      instead, then you can refer to the second item with arr[0][1], arr[1][1], etc.

      Edit: obs, too late and after a better reply too ;(

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        yep ... that's the array-solution :) ... where you refer by index ... the object solution allows to refer by name ... now its up to you to choose one :)

        kind regards

        Comment

        • wangers16
          New Member
          • Jul 2007
          • 57

          #5
          thank you guys for your help :)

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            You're welcome. So which one did you choose? ;)

            Comment

            • wangers16
              New Member
              • Jul 2007
              • 57

              #7
              I chose your one acoder ;)

              Comment

              Working...