0

I am trying to rearrange elements in an array of arrays but have been unsucessful. Can anyone offer suggestions? Here are two options I have tried. I want to swap/switch places for the first and second elements.

arr1 is an array of arrays (i.e. arr[][]) so I created arr2 to be an updated arr1

var arr2 = [];

for (var n = 0; n <arr1.length; n++){
  arr2[n][0] = arr1[n][1];
  arr2[n][1] = arr1[n][0];

}

The other thing I tried was:

function order(arr[]){
  return [arr[n][1],arr[n][0], arr[n][2], arr[n][3]];
}

var arr2 = order(arr1);

3 Answers 3

2

You also need to create a new array for each item:

var arr2 = [];
for(var n = 0; n < arr1.length; n++) {
  arr2[n] = [arr1[n][1], arr1[n][0]];
}
Sign up to request clarification or add additional context in comments.

4 Comments

Does this work if there are more than two elements for arr1[n]? i.e. there is an arr1[n][2] and arr1[n][3]
@djfkdjfkd39939 This approach will work, but you would have to modify the code a bit. This code is for arrays with fixed size 2.
How can you modify this with more than two elements? just listing all the elements?
@djfkdjfkd39939 Depends on what order you want, and how many elements you need.
1

it's quite easy:

var a = arr1[n][0];
arr2[n][0] = arr1[n][1];
arr2[n][1] = a;

you need to save the first value as a variable, because if you do as you did(arr2[n][0] = arr1[n][1];), your two array indexes will have the same value.

You did:

a = 1, b = 2;
a = b;
b = a;

Which resolves in a = 2, b = 2

Also, your code as it is now, doesn't work. You need to create a new array for the simulation of multidimensional arrays in javascript.

for(i = 0; i < (yourdesiredamountofarrays); i++)
{
    yourarray[i] = new Array();
}

2 Comments

there is no need to use a temporary variable, arr1 isn't overridden
Sorry, I think you are mistaking. If he assigns arr[1] to arr[0] and then arr[0] to arr[1], then arr[1] will be the same as arr[0].
1

The first example you need to use a temporary variable for the switch:

var arr2 = [];

for (var n = 0; n <arr1.length; n++){
  var tempVal = arr1[n][1];
  arr2[n][1] = arr1[n][0];
  arr2[n][0] = tempArr;
}

The second example, in JS the variable shouldn't have brackets next to it, as it's just a loosely typed variable name.

function order(arr){
  return [arr[n][1],arr[n][0], arr[n][2], arr[n][3], arr[n][4]];
}

var arr2 = order(arr1);

Next time, before asking you should check the console. The stackoverflow wiki page on JS has lots of great resources for learning to debug JS.

3 Comments

there is no need to use a temporary variable, arr1 isn't overridden
That's what every other answer does. If you have a better way, you should probably share.
James, he is saying "arr1 isn't overriden". There might be other solutions, but this statement is false.

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.