| | |
Adding Array To Another Array
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved
![]() |
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
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.
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.
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.
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.
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Indian Developer
http://falaque.wordpress.com/
•
•
•
•
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.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
function addArr(){ var arr1 = [1,2,3,4]; var arr2 = [5,6,7,8]; arr3=arr1;//or u can use arr1 itself; for(i=arr1.length,j=0;j<arr2.length;i++){ arr3[i]=arr2[j]; j++; } for(i=0;i<arr3.length;i++) document.writeln(arr3[i]); }
Regards ...
IF SOMEONE FEELS THAT THEY HAD NEVER MADE A MISTAKE IN THEIR LIFE, THEN IT MEANS THEY HAD NEVER TRIED A NEW THING IN THEIR LIFE
Just a quick brief regarding arrays. Since an Array can store other Arrays you can get the benefit of multi-dimension arrays.
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.
If we wanted the third index we'd access it this way...
There's no defined limit to how many Arrays you can nest in this manner. For instance...
...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.
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.
Or you can simply do it with the push() method. Code is as follow:
hope it clears you up, and good day to you...
javascript Syntax (Toggle Plain Text)
<script type="text/javascript"> var arr1 = [1,2,3,4]; var arr2 = [5,6,7,8]; for (var x = 0; x <= arr2.length; x++) { arr1.push(arr2[x]) } document.write(arr1 + '<br />') </script>
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- adding to a dynamic array size... (Java)
- Safe Array (C++)
- Adding things to Array-Based Lists (C++)
- assign elements to a multi-d array (C)
- Copying words into an array of char ?!? (C)
- Adding mouse listener to a frame (Java)
- Using a variable as an array's index? (C)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Regarding add new row in the Table.
- Next Thread: Dynamic ID
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate automatically beta box browser bug captchaformproblem checkbox close codes createrange() css cursor debugger dependent disablefirebug dom download dropdown editor element engine enter error events explorer ext file firefox form forms frameworks getselection google gwt gxt hiddenvalue highlightedword hint html htmlform ie7 ie8 iframe internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jsp jump listbox maps masterpage math media menu microsoft mp4 object onmouseoutdivproblem onreadystatechange paypal pdf php player position programming progressbar prototype redirect regex runtime safari scale scriptlets search security select size software sql text textarea unicode w3c window windowofwords windowsxp wysiwyg \n





