EN
JavaScript - move element of array to the last position
0
points
In this article, we would like to show you how to move an element of an array to the last position in JavaScript.
1. By index
In the below examples, we move the element at the index
position to the end of the array
.
// ONLINE-RUNNER:browser;
const array = ['a', 'd', 'b', 'c'];
const index = 1;
array.push(array.splice(index, 1)[0]);
console.log(array); // [ 'a', 'b', 'c', 'd' ]
2. By value
In the below examples, we search for the value
in array
and move it to the last position.
// ONLINE-RUNNER:browser;
const array = ['a', 'd', 'b', 'c'];
const value = 'd';
array.push(array.splice(array.indexOf(value), 1)[0]);
console.log(array); // [ 'a', 'b', 'c', 'd' ]