Array Issue

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

    Array Issue

    Hi there, the question:

    I have this array:

    var Thumbnails = new Array();
    Thumbnails[0] = new Array('1', 'listing_34.jpg ', 'Home View');
    Thumbnails[1] = new Array('2', 'sexy2.jpg', 'Sexy');
    Thumbnails[2] = new Array('3', 'blue-sun-m.jpg', 'New Test');
    Thumbnails[3] = new Array('4', 'viva_firefox_e ats_ie.jpg', 'Firefox');

    All fine at this point. But when a try to change the value for one:

    Thumbnails[0][1] = '2';
    Thumbnails[1][1] = '1';

    This don't work. Any ideas ?, thanks in advanced.

    -- eft0.

  • Mick White

    #2
    Re: Array Issue

    eft0 wrote:[color=blue]
    > Hi there, the question:
    >
    > I have this array:
    >
    > var Thumbnails = new Array();
    > Thumbnails[0] = new Array('1', 'listing_34.jpg ', 'Home View');
    > Thumbnails[1] = new Array('2', 'sexy2.jpg', 'Sexy');
    > Thumbnails[2] = new Array('3', 'blue-sun-m.jpg', 'New Test');
    > Thumbnails[3] = new Array('4', 'viva_firefox_e ats_ie.jpg', 'Firefox');
    >
    > All fine at this point. But when a try to change the value for one:
    >
    > Thumbnails[0][1] = '2';
    > Thumbnails[1][1] = '1';
    >
    > This don't work. Any ideas ?, thanks in advanced.
    >[/color]

    It should work. Unless you declared the variable "Thumbnails " within a
    function, perhaps. How does it not *work*?
    Mick

    Comment

    • eft0

      #3
      Re: Array Issue


      It meant:

      Thumbnails[0][0] = '2';
      Thumbnails[1][0] = '1';



      eft0 wrote:[color=blue]
      > Hi there, the question:
      >
      > I have this array:
      >
      > var Thumbnails = new Array();
      > Thumbnails[0] = new Array('1', 'listing_34.jpg ', 'Home View');
      > Thumbnails[1] = new Array('2', 'sexy2.jpg', 'Sexy');
      > Thumbnails[2] = new Array('3', 'blue-sun-m.jpg', 'New Test');
      > Thumbnails[3] = new Array('4', 'viva_firefox_e ats_ie.jpg', 'Firefox');
      >
      > All fine at this point. But when a try to change the value for one:
      >
      > Thumbnails[0][1] = '2';
      > Thumbnails[1][1] = '1';
      >
      > This don't work. Any ideas ?, thanks in advanced.
      >
      > -- eft0.
      >[/color]

      Comment

      • Lee

        #4
        Re: Array Issue

        eft0 said:[color=blue]
        >
        >Hi there, the question:
        >
        >I have this array:
        >
        >var Thumbnails = new Array();
        >Thumbnails[0] = new Array('1', 'listing_34.jpg ', 'Home View');
        >Thumbnails[1] = new Array('2', 'sexy2.jpg', 'Sexy');
        >Thumbnails[2] = new Array('3', 'blue-sun-m.jpg', 'New Test');
        >Thumbnails[3] = new Array('4', 'viva_firefox_e ats_ie.jpg', 'Firefox');
        >
        >All fine at this point. But when a try to change the value for one:
        >
        >Thumbnails[0][1] = '2';
        >Thumbnails[1][1] = '1';
        >
        >This don't work. Any ideas ?, thanks in advanced.[/color]

        What does "this don't work" mean?
        Are you sure you don't really want to change Thumbnails[0][0] and [1][0]?
        Those are the elements that are initially numeric values.

        Comment

        • Lee

          #5
          Re: Array Issue

          eft0 said:[color=blue]
          >
          >
          >It meant:
          >
          >Thumbnails[0][0] = '2';
          >Thumbnails[1][0] = '1';
          >[/color]

          Are you saying that you found your mistake, and now it works,
          or that this is what you were really doing, and it still doesn't
          work?

          If the latter, then please don't retype code that "doesn't work".
          Copy and paste the exact code, or we'll never find your typo.

          Comment

          • Richard

            #6
            Re: Array Issue

            On Fri, 04 Feb 2005 18:20:03 -0300 eft0 wrote:
            [color=blue]
            > Hi there, the question:[/color]
            [color=blue]
            > I have this array:[/color]
            [color=blue]
            > var Thumbnails = new Array();
            > Thumbnails[0] = new Array('1', 'listing_34.jpg ', 'Home View');
            > Thumbnails[1] = new Array('2', 'sexy2.jpg', 'Sexy');
            > Thumbnails[2] = new Array('3', 'blue-sun-m.jpg', 'New Test');
            > Thumbnails[3] = new Array('4', 'viva_firefox_e ats_ie.jpg', 'Firefox');[/color]
            [color=blue]
            > All fine at this point. But when a try to change the value for one:[/color]
            [color=blue]
            > Thumbnails[0][1] = '2';
            > Thumbnails[1][1] = '1';[/color]
            [color=blue]
            > This don't work. Any ideas ?, thanks in advanced.[/color]
            [color=blue]
            > -- eft0.[/color]



            Me thinks you're trying to do some things you should not be doing.
            As I understand it, the way you show, just ain't kosher.
            Thumbnails[0]="images/name1.jpg"
            This is a ONE dimensional array to begin with.
            You can have var Thumbnails="new Array("name1.jp g", "name2.jpg" ,
            "name3.jpg" )
            But not Thumbnails[0]= new Array( what, ever)

            Now you want to compound the issue by using a 2 dimensional array to do
            what?
            Thumbnails[0][0]="name1.jpg"


            What you may want to try is something like this:

            Thumbnails1=new Array('1', 'listing_34.jpg ', 'Home View');
            Thumbnails2=new Array('2', 'sexy2.jpg', 'Sexy');

            ThumbChoice=new Array()
            ThumbChoice[0][0]=thumbnails1
            ThumbChoice[0][1]=thumbnails2


            Comment

            • Randy Webb

              #7
              Re: Array Issue

              Richard wrote:
              [color=blue]
              > On Fri, 04 Feb 2005 18:20:03 -0300 eft0 wrote:
              >
              >[color=green]
              >>Hi there, the question:[/color]
              >
              >[color=green]
              >>I have this array:[/color]
              >
              >[color=green]
              >>var Thumbnails = new Array();
              >>Thumbnails[0] = new Array('1', 'listing_34.jpg ', 'Home View');
              >>Thumbnails[1] = new Array('2', 'sexy2.jpg', 'Sexy');
              >>Thumbnails[2] = new Array('3', 'blue-sun-m.jpg', 'New Test');
              >>Thumbnails[3] = new Array('4', 'viva_firefox_e ats_ie.jpg', 'Firefox');[/color]
              >
              >[color=green]
              >>All fine at this point. But when a try to change the value for one:[/color]
              >
              >[color=green]
              >>Thumbnails[0][1] = '2';
              >>Thumbnails[1][1] = '1';[/color]
              >
              >[color=green]
              >>This don't work. Any ideas ?, thanks in advanced.[/color]
              >
              >[color=green]
              >>-- eft0.[/color]
              >
              >
              >
              >
              > Me thinks you're trying to do some things you should not be doing.[/color]

              Coming from you, thats funny.
              [color=blue]
              > As I understand it, the way you show, just ain't kosher.
              > Thumbnails[0]="images/name1.jpg"
              > This is a ONE dimensional array to begin with.[/color]

              There are no other kinds of Arrays in ECMAScript.
              [color=blue]
              > You can have var Thumbnails="new Array("name1.jp g", "name2.jpg" ,
              > "name3.jpg" )
              > But not Thumbnails[0]= new Array( what, ever)[/color]

              Ummm, YES you can. If you would bother learning the subject before
              offering advice on the subject you might understand that.
              [color=blue]
              > Now you want to compound the issue by using a 2 dimensional array to do
              > what?
              > Thumbnails[0][0]="name1.jpg"[/color]

              Thats not a "2 dimensional array", it is a nested array. Learn the
              difference.
              [color=blue]
              >
              > What you may want to try is something like this:
              >
              > Thumbnails1=new Array('1', 'listing_34.jpg ', 'Home View');
              > Thumbnails2=new Array('2', 'sexy2.jpg', 'Sexy');
              >
              > ThumbChoice=new Array()
              > ThumbChoice[0][0]=thumbnails1
              > ThumbChoice[0][1]=thumbnails2[/color]

              Not withstanding your typos which would throw a "thumbnails 1 is not
              defined" error message, all you have done is create a nested array with
              extra overhead.

              Before you continue to give advice in this group, please at least read
              the group, buy some books, do a lot of reading and get a basic grasp of
              the language before you attempt to give answers.

              --
              Randy
              comp.lang.javas cript FAQ - http://jibbering.com/faq
              Answer:It destroys the order of the conversation
              Question: Why?
              Answer: Top-Posting.
              Question: Whats the most annoying thing on Usenet?

              Comment

              • Lee

                #8
                Re: Array Issue

                Richard said:[color=blue]
                >
                >On Fri, 04 Feb 2005 18:20:03 -0300 eft0 wrote:
                >[color=green]
                >> Hi there, the question:[/color]
                >[color=green]
                >> I have this array:[/color]
                >[color=green]
                >> var Thumbnails = new Array();
                >> Thumbnails[0] = new Array('1', 'listing_34.jpg ', 'Home View');
                >> Thumbnails[1] = new Array('2', 'sexy2.jpg', 'Sexy');
                >> Thumbnails[2] = new Array('3', 'blue-sun-m.jpg', 'New Test');
                >> Thumbnails[3] = new Array('4', 'viva_firefox_e ats_ie.jpg', 'Firefox');[/color]
                >[color=green]
                >> All fine at this point. But when a try to change the value for one:[/color]
                >[color=green]
                >> Thumbnails[0][1] = '2';
                >> Thumbnails[1][1] = '1';[/color]
                >[color=green]
                >> This don't work. Any ideas ?, thanks in advanced.[/color]
                >[color=green]
                >> -- eft0.[/color]
                >
                >
                >
                >Me thinks you're trying to do some things you should not be doing.
                >As I understand it, the way you show, just ain't kosher.
                >Thumbnails[0]="images/name1.jpg"
                >This is a ONE dimensional array to begin with.
                >You can have var Thumbnails="new Array("name1.jp g", "name2.jpg" ,
                >"name3.jpg" )
                >But not Thumbnails[0]= new Array( what, ever)
                >
                >Now you want to compound the issue by using a 2 dimensional array to do
                >what?
                >Thumbnails[0][0]="name1.jpg"
                >
                >
                >What you may want to try is something like this:
                >
                >Thumbnails1=ne w Array('1', 'listing_34.jpg ', 'Home View');
                >Thumbnails2=ne w Array('2', 'sexy2.jpg', 'Sexy');
                >
                >ThumbChoice=ne w Array()
                >ThumbChoice[0][0]=thumbnails1
                >ThumbChoice[0][1]=thumbnails2[/color]

                The OP's code as posted is perfectly valid.
                Yours is not.

                You refer to ThumbChoice[0] as if it were an Array without
                assigning an array reference to it.

                Try this stragegy:
                Read the post.
                Test the code in the post.
                If necessary, correct the code.
                Test the code again.
                Post comments about the code, possibly including new, tested code.

                Comment

                • Douglas Crockford

                  #9
                  Re: Array Issue

                  > I have this array:[color=blue]
                  >
                  > var Thumbnails = new Array();
                  > Thumbnails[0] = new Array('1', 'listing_34.jpg ', 'Home View');
                  > Thumbnails[1] = new Array('2', 'sexy2.jpg', 'Sexy');
                  > Thumbnails[2] = new Array('3', 'blue-sun-m.jpg', 'New Test');
                  > Thumbnails[3] = new Array('4', 'viva_firefox_e ats_ie.jpg', 'Firefox');
                  >
                  > All fine at this point. But when a try to change the value for one:
                  >
                  > Thumbnails[0][1] = '2';
                  > Thumbnails[1][1] = '1';
                  >
                  > This don't work. Any ideas ?, thanks in advanced.[/color]

                  "This don't work" is not a useful description of a problem. I do have
                  some ideas though.

                  An array of arrays is not the best data structure for this application.
                  An array of objects is better.

                  var Thumbnails = [
                  {number: '1', url: 'listing_34.jpg ', title: 'Home View'},
                  {number: '2', url: 'sexy2.jpg', title: 'Sexy'},
                  {number: '3', url: 'blue-sun-m.jpg', title: 'New Test'},
                  {number: '4', url: 'viva_firefox_e ats_ie.jpg',
                  title: 'Firefox'}];

                  Now it is much clearer what you will be accessing and modifying as you
                  reach into the structures.

                  Thumbnails[0].number = '2';
                  Thumbnails[1].number = '1';

                  If you use an array inappropriately , you could easily be clobbering the
                  url when you are intending to change the number.


                  Comment

                  Working...