Languages
[Edit]
EN

JavaScript - move element of array to the last position

0 points
Created by:
Nathanial-Donald
584

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' ]

 

See also

  1. JavaScript - move element of array to the front

References

  1. Array.prototype.indexOf() - JavaScript | MDN
  2. Array.prototype.splice() - JavaScript | MDN

Alternative titles

  1. JavaScript - move element of array to the end
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join