/    Sign up×
Community /Pin to ProfileBookmark

Array.concat() and it’s uses…

I’m just wondering if anyone can help me with an issue:

Say I have either of the examples below:

var fruits = new Array(“lemon”, “apple”, “orange”, “lime”)

or

var fruits = new Array()
fruits[0] = “lemon”;
fruits[1] = “apple”;
fruits[2] = “orange”;
fruits[3] = “lime”;

Now, why, if I use the concat(), do I receive an “unidentified” attached to the end?

fruits[0].concat(fruits[3]);

I’m looking for “lemonlime”, not “lemonlimeunidentified”…

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@mrhooSep 11.2007 — var fruits =["lemon", "apple", "orange", "lime"];

alert(fruits[0].concat(fruits[3]));

I have no problem with this in IE7,Opera9,or Firefox2-

I had to check, I always use fruits[0][B]+[/B] fruits[3].
Copy linkTweet thisAlerts:
@WebTechauthorSep 11.2007 — mrhoo

did you try the second example to see if it would work?

I was using:

document.write(fruits[0].concat(fruits[3]);
Copy linkTweet thisAlerts:
@mrhooSep 11.2007 — var fruits = new Array()

fruits[0] = "lemon";

fruits[1] = "apple";

fruits[2] = "orange";

fruits[3] = "lime";

alert(fruits[0].concat(fruits[3]));

I get lemonlime for the alert and for document.write...
Copy linkTweet thisAlerts:
@WebTechauthorSep 11.2007 — try the same with "document.write();". maybe that's where I'm having the problem...
Copy linkTweet thisAlerts:
@mrhooSep 12.2007 — This has nothing to do with your problem, which I can't reproduce, but your title is wrong. You are using String.concat, not Array.concat.

Array.concat returns a new Array by chaining Arrays together, end to end.

var A=[1,2,3];

var B=[3,2,1];

var C= A.concat(?;

A and B still have their original values,

C=[1,2,3,3,2,1];

String has its own concat, which you are using on individual Array members.

Same as using '+'.
×

Success!

Help @WebTech spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...