What is wrong with array.concat in this small example

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • F. Da Costa

    What is wrong with array.concat in this small example

    Hi,

    Could it be correct that the following code does *not* work because i'm not
    using the var arr = new Array("a","b"," c"); methodology??

    Read through

    but there was no mention of particular constructors having to be used.

    And if so how does one resolve it (if resolvable)?

    TIA
    Fermin DCG

    =============== ===========
    var famname = new Array();
    famname[0] = "Jan 0 Egil";
    famname[1] = "To1ve";
    famname[2] = "He2ge";
    famname[3] = "Sta3le";
    famname[4] = "Kai 4Jim";
    famname[5] = "Bor5ge";

    var famname2 = new Array(6);
    famname2[0] = "Jan Egil2";
    famname2[1] = "Tove2";
    famname2[2] = "Hege2";
    famname2[3] = "Stale2";
    famname2[4] = "Kai Jim2";
    famname2[5] = "Borge2";

    famname = famname.concat( famname2); *Does not produce the concatenation!*

    for (var i=0; i<6; i++) {
    document.write( famname[i] + "<br />");
    }

    for (var i=0; i<6; i++) {
    document.write( famname2[i] + "<br />");
    }
  • McKirahan

    #2
    Re: What is wrong with array.concat in this small example

    "F. Da Costa" <dacosta@xs4all .nl> wrote in message
    news:40001a98$0 $321$e4fe514c@n ews.xs4all.nl.. .[color=blue]
    > Hi,
    >
    > Could it be correct that the following code does *not* work because i'm[/color]
    not[color=blue]
    > using the var arr = new Array("a","b"," c"); methodology??
    >
    > Read through
    >[/color]

    ray.html#119482 7[color=blue]
    > but there was no mention of particular constructors having to be used.
    >
    > And if so how does one resolve it (if resolvable)?
    >
    > TIA
    > Fermin DCG
    >
    > =============== ===========
    > var famname = new Array();
    > famname[0] = "Jan 0 Egil";
    > famname[1] = "To1ve";
    > famname[2] = "He2ge";
    > famname[3] = "Sta3le";
    > famname[4] = "Kai 4Jim";
    > famname[5] = "Bor5ge";
    >
    > var famname2 = new Array(6);
    > famname2[0] = "Jan Egil2";
    > famname2[1] = "Tove2";
    > famname2[2] = "Hege2";
    > famname2[3] = "Stale2";
    > famname2[4] = "Kai Jim2";
    > famname2[5] = "Borge2";
    >
    > famname = famname.concat( famname2); *Does not produce the concatenation!*[/color]


    Your using "famname" as an array and as a string. Try:

    var famname3 = famname.concat( famname2);


    Comment

    • F. Da Costa

      #3
      Re: What is wrong with array.concat in this small example

      McKirahan wrote:
      [color=blue]
      > "F. Da Costa" <dacosta@xs4all .nl> wrote in message
      > news:40001a98$0 $321$e4fe514c@n ews.xs4all.nl.. .
      >[color=green]
      >>Hi,
      >>
      >>Could it be correct that the following code does *not* work because i'm[/color]
      >
      > not
      >[color=green]
      >>using the var arr = new Array("a","b"," c"); methodology??
      >>
      >>Read through
      >>[/color]
      >
      > http://devedge.netscape.com/library/...5/reference/ar
      > ray.html#119482 7
      >[color=green]
      >>but there was no mention of particular constructors having to be used.
      >>
      >>And if so how does one resolve it (if resolvable)?
      >>
      >>TIA
      >>Fermin DCG
      >>
      >>============= =============
      >>var famname = new Array();
      >>famname[0] = "Jan 0 Egil";
      >>famname[1] = "To1ve";
      >>famname[2] = "He2ge";
      >>famname[3] = "Sta3le";
      >>famname[4] = "Kai 4Jim";
      >>famname[5] = "Bor5ge";
      >>
      >>var famname2 = new Array(6);
      >>famname2[0] = "Jan Egil2";
      >>famname2[1] = "Tove2";
      >>famname2[2] = "Hege2";
      >>famname2[3] = "Stale2";
      >>famname2[4] = "Kai Jim2";
      >>famname2[5] = "Borge2";
      >>
      >>famname = famname.concat( famname2); *Does not produce the concatenation!*[/color]
      >
      >
      >
      > Your using "famname" as an array and as a string. Try:
      >
      > var famname3 = famname.concat( famname2);[/color]
      That did the trick indeed.
      Makes me wonder from time 2 time.

      Thx a lot

      Cheers
      [color=blue]
      >
      >[/color]

      Comment

      • Grant Wagner

        #4
        Re: What is wrong with array.concat in this small example

        "F. Da Costa" wrote:
        [color=blue]
        > Hi,
        >
        > Could it be correct that the following code does *not* work because i'm not
        > using the var arr = new Array("a","b"," c"); methodology??
        >
        > Read through
        > http://devedge.netscape.com/library/...y.html#1194827
        > but there was no mention of particular constructors having to be used.
        >
        > And if so how does one resolve it (if resolvable)?
        >
        > TIA
        > Fermin DCG
        >
        > =============== ===========
        > var famname = new Array();
        > famname[0] = "Jan 0 Egil";
        > famname[1] = "To1ve";
        > famname[2] = "He2ge";
        > famname[3] = "Sta3le";
        > famname[4] = "Kai 4Jim";
        > famname[5] = "Bor5ge";
        >
        > var famname2 = new Array(6);
        > famname2[0] = "Jan Egil2";
        > famname2[1] = "Tove2";
        > famname2[2] = "Hege2";
        > famname2[3] = "Stale2";
        > famname2[4] = "Kai Jim2";
        > famname2[5] = "Borge2";
        >
        > famname = famname.concat( famname2); *Does not produce the concatenation!*
        >
        > for (var i=0; i<6; i++) {
        > document.write( famname[i] + "<br />");
        > }
        >
        > for (var i=0; i<6; i++) {
        > document.write( famname2[i] + "<br />");
        > }[/color]

        Of COURSE it concatenates the arrays, but if you look at your "test code" you are simply looping
        through 6 elements of the first array and 6 elements of the second array and outputting them.

        If you modify your test code to actually output all of the array values, you'll see that famname
        contains all the entries from both famname and famname2:

        document.write( "Contents of famname<br />");
        for (var i=0; i<famname.lengt h; i++) {
        document.write( famname[i] + "<br />");
        }

        document.write( "Contents of famname2<br />"):
        for (var i=0; i<famname2; i++) {
        document.write( famname2[i] + "<br />");
        }

        Better yet:

        document.write( "<p>Content s of famname<br />" + famname.join("< br />") + "</p>");
        document.write( "<p>Content s of famname2<br />" + famname2.join(" <br />") + "</p>");

        --
        | Grant Wagner <gwagner@agrico reunited.com>

        * Client-side Javascript and Netscape 4 DOM Reference available at:
        * http://devedge.netscape.com/library/...ce/frames.html
        * Internet Explorer DOM Reference available at:
        * http://msdn.microsoft.com/workshop/a...ence_entry.asp
        * Netscape 6/7 DOM Reference available at:
        * http://www.mozilla.org/docs/dom/domref/
        * Tips for upgrading JavaScript for Netscape 7 / Mozilla
        * http://www.mozilla.org/docs/web-deve...upgrade_2.html


        Comment

        • Grant Wagner

          #5
          Re: What is wrong with array.concat in this small example

          McKirahan wrote:
          [color=blue]
          > "F. Da Costa" <dacosta@xs4all .nl> wrote in message
          > news:40001a98$0 $321$e4fe514c@n ews.xs4all.nl.. .[color=green]
          > > Hi,
          > >
          > > Could it be correct that the following code does *not* work because i'm[/color]
          > not[color=green]
          > > using the var arr = new Array("a","b"," c"); methodology??
          > >
          > > Read through
          > >[/color]
          > http://devedge.netscape.com/library/...5/reference/ar
          > ray.html#119482 7[color=green]
          > > but there was no mention of particular constructors having to be used.
          > >
          > > And if so how does one resolve it (if resolvable)?
          > >
          > > TIA
          > > Fermin DCG
          > >
          > > =============== ===========
          > > var famname = new Array();
          > > famname[0] = "Jan 0 Egil";
          > > famname[1] = "To1ve";
          > > famname[2] = "He2ge";
          > > famname[3] = "Sta3le";
          > > famname[4] = "Kai 4Jim";
          > > famname[5] = "Bor5ge";
          > >
          > > var famname2 = new Array(6);
          > > famname2[0] = "Jan Egil2";
          > > famname2[1] = "Tove2";
          > > famname2[2] = "Hege2";
          > > famname2[3] = "Stale2";
          > > famname2[4] = "Kai Jim2";
          > > famname2[5] = "Borge2";
          > >
          > > famname = famname.concat( famname2); *Does not produce the concatenation!*[/color]
          >
          > Your using "famname" as an array and as a string. Try:[/color]

          No, he really isn't. Array.concat(Ar ray) returns an Array, so

          famname = famname.concat( famname2); concatenates famnam2 to famname, returns the
          result, and assigns it to famname. Even *if* this were the case, JavaScript
          wouldn't care, it will accept all sorts of new type assignments:

          var a = [ "a", "b", "c" ];
          a = a.join("<br />");
          document.write( a);

          a starts life as an Array, a join() is performed on the contents of that Array
          and a String is returned, which is assigned to the same variable a. That
          variable is then output.

          And it works just fine.

          I could provide numerous other examples of assigning different variable types to
          the same variable name, but the one above is on point for your concern with his
          original code.
          [color=blue]
          > var famname3 = famname.concat( famname2);[/color]

          His problem has nothing to do with the assignment, it had to do with his test
          output. If you look, he outputs only the first 6 elements of the Array pointed
          to by famname. It contains 12 elements (6 from famname, 6 from famname2), but
          you never see the 2nd set of 6 elements, because he simply doesn't output them.

          He should be using:

          famname = [ "a", "b", "c" ];
          famname2 = [ "d", "e", "f" ];
          famname = famname.concat( famname2);
          document.write( '<p>famname contains:<br />' + famname.join('< br />') + '</p>');

          --
          | Grant Wagner <gwagner@agrico reunited.com>

          * Client-side Javascript and Netscape 4 DOM Reference available at:
          *


          * Internet Explorer DOM Reference available at:
          *
          Gain technical skills through documentation and training, earn certifications and connect with the community


          * Netscape 6/7 DOM Reference available at:
          * http://www.mozilla.org/docs/dom/domref/
          * Tips for upgrading JavaScript for Netscape 7 / Mozilla
          * http://www.mozilla.org/docs/web-deve...upgrade_2.html


          Comment

          • F. Da Costa

            #6
            Re: What is wrong with array.concat in this small example

            Grant Wagner wrote:
            [color=blue]
            > "F. Da Costa" wrote:
            >
            >[color=green]
            >>Hi,
            >>
            >>Could it be correct that the following code does *not* work because i'm not
            >>using the var arr = new Array("a","b"," c"); methodology??
            >>
            >>Read through
            >>http://devedge.netscape.com/library/...y.html#1194827
            >>but there was no mention of particular constructors having to be used.
            >>
            >>And if so how does one resolve it (if resolvable)?
            >>
            >>TIA
            >>Fermin DCG
            >>
            >>============= =============
            >>var famname = new Array();
            >>famname[0] = "Jan 0 Egil";
            >>famname[1] = "To1ve";
            >>famname[2] = "He2ge";
            >>famname[3] = "Sta3le";
            >>famname[4] = "Kai 4Jim";
            >>famname[5] = "Bor5ge";
            >>
            >>var famname2 = new Array(6);
            >>famname2[0] = "Jan Egil2";
            >>famname2[1] = "Tove2";
            >>famname2[2] = "Hege2";
            >>famname2[3] = "Stale2";
            >>famname2[4] = "Kai Jim2";
            >>famname2[5] = "Borge2";
            >>
            >>famname = famname.concat( famname2); *Does not produce the concatenation!*
            >>
            >>for (var i=0; i<6; i++) {
            >>document.writ e(famname[i] + "<br />");
            >>}
            >>
            >>for (var i=0; i<6; i++) {
            >>document.writ e(famname2[i] + "<br />");
            >>}[/color]
            >
            >
            > Of COURSE it concatenates the arrays, but if you look at your "test code" you are simply looping
            > through 6 elements of the first array and 6 elements of the second array and outputting them.[/color]
            :) The infamous middle of the night error.
            Yep you are very correct. Obviously resolved this whole thing and knee-high
            in other muck again.

            Thx for the reaction anyway.
            [color=blue]
            >
            > If you modify your test code to actually output all of the array values, you'll see that famname
            > contains all the entries from both famname and famname2:
            >
            > document.write( "Contents of famname<br />");
            > for (var i=0; i<famname.lengt h; i++) {
            > document.write( famname[i] + "<br />");
            > }
            >
            > document.write( "Contents of famname2<br />"):
            > for (var i=0; i<famname2; i++) {
            > document.write( famname2[i] + "<br />");
            > }
            >
            > Better yet:
            >
            > document.write( "<p>Content s of famname<br />" + famname.join("< br />") + "</p>");
            > document.write( "<p>Content s of famname2<br />" + famname2.join(" <br />") + "</p>");
            >
            > --
            > | Grant Wagner <gwagner@agrico reunited.com>
            >
            > * Client-side Javascript and Netscape 4 DOM Reference available at:
            > * http://devedge.netscape.com/library/...ce/frames.html
            > * Internet Explorer DOM Reference available at:
            > * http://msdn.microsoft.com/workshop/a...ence_entry.asp
            > * Netscape 6/7 DOM Reference available at:
            > * http://www.mozilla.org/docs/dom/domref/
            > * Tips for upgrading JavaScript for Netscape 7 / Mozilla
            > * http://www.mozilla.org/docs/web-deve...upgrade_2.html
            >
            >[/color]

            Comment

            Working...