468,133 Members | 1,509 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,133 developers. It's quick & easy.

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
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 />");
}
Jul 20 '05 #1
5 1611
"F. Da Costa" <da*****@xs4all.nl> wrote in message
news:40*********************@news.xs4all.nl...
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/...5/reference/ar
ray.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!*

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

var famname3 = famname.concat(famname2);
Jul 20 '05 #2
McKirahan wrote:
"F. Da Costa" <da*****@xs4all.nl> wrote in message
news:40*********************@news.xs4all.nl...
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/...5/reference/ar
ray.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!*


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

var famname3 = famname.concat(famname2);

That did the trick indeed.
Makes me wonder from time 2 time.

Thx a lot

Cheers

Jul 20 '05 #3
"F. Da Costa" wrote:
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 />");
}


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.length; 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>Contents of famname<br />" + famname.join("<br />") + "</p>");
document.write("<p>Contents of famname2<br />" + famname2.join("<br />") + "</p>");

--
| Grant Wagner <gw*****@agricoreunited.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
Jul 20 '05 #4
McKirahan wrote:
"F. Da Costa" <da*****@xs4all.nl> wrote in message
news:40*********************@news.xs4all.nl...
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/...5/reference/ar
ray.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!*


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


No, he really isn't. Array.concat(Array) 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.
var famname3 = famname.concat(famname2);


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 <gw*****@agricoreunited.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
Jul 20 '05 #5
Grant Wagner wrote:
"F. Da Costa" wrote:

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 />");
}

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.

:) 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.

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.length; 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>Contents of famname<br />" + famname.join("<br />") + "</p>");
document.write("<p>Contents of famname2<br />" + famname2.join("<br />") + "</p>");

--
| Grant Wagner <gw*****@agricoreunited.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

Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by F. Da Costa | last post: by
5 posts views Thread by Andrew Poulos | last post: by
2 posts views Thread by vvenk | last post: by
3 posts views Thread by Mythran | last post: by
7 posts views Thread by Leonel Gayard | last post: by
6 posts views Thread by Jeff Gardner | last post: by
1 post views Thread by agendum97 | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.