how to swap two elements in an array js
// Since ES6
[a[0], a[1]] = [a[1], a[0]]
how to swap two elements in an array js
// Since ES6
[a[0], a[1]] = [a[1], a[0]]
swap elements in array javascript
const heavy = ["Rammstein", "Motorhead", "Imsomnium", "Be´lakor"];
let aux = "";
const intercambiar = (arr, i1, i2) => {
//Guardo en un provisional el valor del primer indice
aux = arr[i1];
//Inserte el valor del segundo índice en el lugar del primer índice
arr[i1] = arr[i2];
// Al hacer esto pierdo el valor original pero lo tengo guardado en aux por
//lo que solo necesito igualar el segundo índice a aux para completar
//el ejercicio.
arr[i2] = aux;
return arr;
};
let resulIntercambiar = intercambiar(heavy, 1, 2);
console.log(resulIntercambiar);
javascript swap array elements
var b = list[y];
list[y] = list[x];
list[x] = b;
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us