Languages
[Edit]
EN

JavaScript - move array element from one position to another

0 points
Created by:
ArcadeParade
666

In this article, we would like to show you how to move an array element from one position to another in JavaScript.

Practical examples

Edit

Example 1

Edit

In this example, we create a reusable arrow function that uses splice() method to move array element from indexOld position to indexNew.

Note: When indexNew is greater or equal to array.length the remaining space will be filled with undefined values (go to the Example 2).

Example 2

Edit

In this section, we present how the moveElement() method works when indexNew is greater than array.length value.

 

References

Edit
  1. Array.prototype.splice() - JavaScript | MDN

Alternative titles

  1. JavaScript - move array element from one index to another
  2. JavaScript - move array item from one index to another
  3. JavaScript - change index of array item
  4. JavaScript - change index of array element
1
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