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

0

1 Answer 1

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.