0

I have an array called arr1 and a second array called arr2. Both are created dinamically (I mean arr2 is different every time enters a while and arr1 it's the same but is filled everytime by arr2 new array).

I want every postion of arr1 to have an different array (in the example arr2 is always the same but nevermind that), how can I do this?

I've tried this way:

//Some context
arr1[x] = new Array();
arr2[y] = new Array();
while(j < 10){
  arr2[j] = j;
  j++;
}
while(x < 10){
  arr1[x] = [arr2]; //problem
  x++;
}


arr1[x] = new Array(otherArr.length);

arr1[x] = [arr2]  (?)

arr1[x] = arr2 (?)

arr1[x] = new Array(arr2.length)
arr1[x] = arr2 (?)
6
  • 1
    Do you really want arr2 to be at every position or just copies of arr2 to be at every position. Think carefully, they are different things. Commented Oct 31, 2012 at 12:46
  • You want to arr2 to be accesible using all indexes of arr1? Is that right? Commented Oct 31, 2012 at 12:48
  • Thank you for showing us what you have tried. It would be helpful if you would show us what sort of code you want to work. For example, after you perform this 'magic', will you then modify arr2 and expect arr1 to be changed? Are you talking about a two-dimensional array, or just equality? What are you trying to do? Commented Oct 31, 2012 at 12:52
  • I've edit the question. Please, take a look. Commented Oct 31, 2012 at 12:55
  • I want every postion of arr1 to have an different array Commented Oct 31, 2012 at 13:01

3 Answers 3

3
for(var i=0; i<arr1.length; i++)
  arr1[i] = arr2;

Edit: use for(var i=0; i<arr1.length; i++) arr1[i] = arr2.slice(0); If you want to copy the array instead of using a reference.

Sign up to request clarification or add additional context in comments.

2 Comments

This assumes that you wanted a pseudo-two-dimensional array as the result, where you can then use arr1[3][7] to get one of the eighth items of arr2. However, it also means that any modification to any value in one column will affect others. Is that what you want?
I want every postion of arr1 to have an independent array;
1

If array1 has length > 0 then you can use this method to replace each position in array1 with array2.

array1 = ["","",""]
array2 = [1,2,3]

for (var i=0; i<array1.length; i++)
  {
  array1[i] = array2;
  }

1 Comment

The length doesn't have to be > 0. Your loop just doesn't get executed when it's 0.
0

In case you want to use arr2 as a template, such that later modifications to one 'row' don't affect others:

var arr1 = [1,2,3];
var arr2 = [4,5,6];

for (var i=arr1.length;i--;) arr1[i] = arr2.concat(); // create a copy

At this point you have the equivalent of:

var arr1 = [
  [4,5,6],
  [4,5,6],
  [4,5,6]
];

…such that you can change arr1[0][0] = 0 and see:

[
  [0,5,6],
  [4,5,6],
  [4,5,6]
]

instead of

[
  [0,5,6],
  [0,5,6],
  [0,5,6]
]

as some other answers here will cause.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.