+ 1

Is there a method to switch an array content position with another one in the same array?

badically let's say I have an array like this: myArray = [1,0,0,0]; and I want it to switch the position of the 1 with one of the zeroes: [0,1,0,0] is there a method or function I can use or it's only manual.

20th Oct 2017, 6:06 AM
PeterMx
PeterMx - avatar
4 ответов
+ 2
20th Oct 2017, 6:49 AM
$machitgarha
$machitgarha - avatar
+ 3
It may be complex, but there is a short way: [myArray[0], myArray[1]] = [myArray[1], myArray[0]];
20th Oct 2017, 6:27 AM
$machitgarha
$machitgarha - avatar
+ 2
I fixed my code. It's now compatible with all browsers.
20th Oct 2017, 4:53 PM
$machitgarha
$machitgarha - avatar
0
Thanks a lot but I couldn't run the code, probably my browser is too old. But your first answer mayor be useful. Edit:managed to get your code running there were some extra [ ] in the if statement and had to change let for a variable.
20th Oct 2017, 2:07 PM
PeterMx
PeterMx - avatar