943,810 Members | Top Members by Rank

Ad:
Nov 22nd, 2008
0

Adding Array To Another Array

Expand Post »
I need to add an array to the end of another array. I understand how to do this using the concat, join, and push methods. However, I need to do this using a loop (I am pretty sure a 'for' loop is the right choice), however, I can not figure out how to do this with a for loop.

arr1 = [1,2,3,4]
arr2 = [5,6,7,8]

All I need to do is add arr2 on to the end of arr1 using a loop to do this, but I have not been able to figure out how to do this. Does anyone have any suggestions or can someone please lead me in the right direction. Thanks in advance for any suggestions and/or assistance.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
jobojo is offline Offline
28 posts
since Oct 2008
Nov 22nd, 2008
0

Re: Adding Array To Another Array

Hi,
define a new array with function: arr3=new Array(no_of_element);
then use for loop to populate member of both array in to it as u want.

use arr_var[index] to access individual member of array.
Reputation Points: 165
Solved Threads: 59
Posting Pro in Training
DangerDev is offline Offline
485 posts
since Jan 2008
Nov 22nd, 2008
0

Re: Adding Array To Another Array

Click to Expand / Collapse  Quote originally posted by DangerDev ...
Hi,
define a new array with function: arr3=new Array(no_of_element);
then use for loop to populate member of both array in to it as u want.

use arr_var[index] to access individual member of array.
This would help:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function addArr(){
  2. var arr1 = [1,2,3,4];
  3. var arr2 = [5,6,7,8];
  4. arr3=arr1;//or u can use arr1 itself;
  5. for(i=arr1.length,j=0;j<arr2.length;i++){
  6. arr3[i]=arr2[j];
  7. j++;
  8. }
  9. for(i=0;i<arr3.length;i++)
  10. document.writeln(arr3[i]);
  11. }

Regards ...
Reputation Points: 10
Solved Threads: 3
Newbie Poster
sasankasekhar is offline Offline
24 posts
since Jan 2007
Nov 22nd, 2008
0

Re: Adding Array To Another Array

Just a quick brief regarding arrays. Since an Array can store other Arrays you can get the benefit of multi-dimension arrays.

var x=[0,1,2,3,4,5]; var y=[x];
In the above example i've created an array named "x" and assigned it as the first element in the array "y". If we ask for the value of y[0] it will return the contents of "x" as a string because we didn't specify an index.

var x=[0,1,2,3,4,5]; var y=[x]; document.writeln(y[0]); // Will output: 0,1,2,3,4,5
If we wanted the third index we'd access it this way...

var x=[0,1,2,3,4,5]; var y=[x]; document.writeln(y[0][3]); // Will output: 2
There's no defined limit to how many Arrays you can nest in this manner. For instance...

document.writeln(bigArray[5][8][12][1])
...would indicate bigArray's 5th index held an array, who's 8th index held an array, who's 12th index held an array, who's first index contains the data we want.
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Nov 22nd, 2008
0

Re: Adding Array To Another Array

Or you can simply do it with the push() method. Code is as follow:
javascript Syntax (Toggle Plain Text)
  1. <script type="text/javascript">
  2. var arr1 = [1,2,3,4];
  3. var arr2 = [5,6,7,8];
  4. for (var x = 0; x <= arr2.length; x++) {
  5. arr1.push(arr2[x]) }
  6. document.write(arr1 + '<br />')
  7. </script>
hope it clears you up, and good day to you...
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Nov 22nd, 2008
0

Re: Adding Array To Another Array

Thank you for all of the responses. I have tried all of the advice and gotten all of it to work for me and I have a better understanding now.

Essential, thanks for the additional tips and information regarding arrays and their usage, it was greatly appreciated. Your final code with the push method was exactly how I was originally trying to figure the problem out and I was very close except I was off by a couple of minor details. Apparently, I was writing the push method portion incorrectly. Thanks again for all of your help and have a wonderful holiday season everyone
Reputation Points: 10
Solved Threads: 0
Light Poster
jobojo is offline Offline
28 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Regarding add new row in the Table.
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: calculate mean function!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC