0

Possible Duplicate:
Javascript swap array elements

For example i have an array var arr = ['one', 'two', 'three']; and i wona to replace 1st key to 2nd and my array should start to look like ['two', 'one', 'three']; how can i do that, it need to me for bubble sorting

Community
  • 1
  • 1
Anton Sementsov
  • 1,196
  • 6
  • 18
  • 34

1 Answers1

1
var arr = ['one', 'two', 'three'],
    temp;

temp = arr[1]; // temp is 'two'
arr[1] = arr[0]; // Now it is ['one', 'one', 'three']
arr[0] = temp; // And now it is ['two', 'one', 'three']

Just use a temporary variable.

Demo

Some Guy
  • 15,854
  • 10
  • 58
  • 67