how to swap two elements in an array js
// Since ES6
[a[0], a[1]] = [a[1], a[0]]var b = list[y];
list[y] = list[x];
list[x] = b;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);
Also in JavaScript:
- get random numbers javascript
- how to write img jsx
- search a word and separate in javascript
- array of images javascript
- npm google map api react
- object values javascript
- react native gifted chat
- how to add oAuth google signin in react native app
- javascript get scroll position
- javascript convert binary to text
- how to make a var and sprite in javascript
- javascript 1 + "1"
- react font awesome
- get element by id in javascript
- js redirect to url
- angular ngclass
- funciones invocan a funciones javascript
- on success replace class div ajax
- w3schools json
- how to clear array in javascript
- js class private
- getelementbyclass
- js run html in blob
- js replace blogger thumb