What is array-move?
The `array-move` npm package allows you to move an item in an array from one position to another. This can be useful for reordering lists or managing the positions of elements in an array.
What are array-move's main functionalities?
Move an item within an array
This feature allows you to move an item from one index to another within the same array. In this example, the item at index 1 ('b') is moved to index 2.
const arrayMove = require('array-move');
const input = ['a', 'b', 'c'];
const result = arrayMove(input, 1, 2);
console.log(result); // ['a', 'c', 'b']
Move an item to the start of an array
This feature allows you to move an item to the start of the array. In this example, the item at index 2 ('c') is moved to index 0.
const arrayMove = require('array-move');
const input = ['a', 'b', 'c'];
const result = arrayMove(input, 2, 0);
console.log(result); // ['c', 'a', 'b']
Move an item to the end of an array
This feature allows you to move an item to the end of the array. In this example, the item at index 0 ('a') is moved to index 2.
const arrayMove = require('array-move');
const input = ['a', 'b', 'c'];
const result = arrayMove(input, 0, 2);
console.log(result); // ['b', 'c', 'a']
Other packages similar to array-move
array-move-item
The `array-move-item` package also allows you to move items within an array. It is similar to `array-move` but may have different performance characteristics or additional utility functions.
array-move
Move an array item to a different position
Install
$ npm install array-move
Usage
import {arrayMoveImmutable} from 'array-move';
const input = ['a', 'b', 'c'];
const array1 = arrayMoveImmutable(input, 1, 2);
console.log(array1);
const array2 = arrayMoveImmutable(input, -1, 0);
console.log(array2);
const array3 = arrayMoveImmutable(input, -2, -3);
console.log(array3);
API
arrayMoveImmutable(array, fromIndex, toIndex)
Clones the given array
, moves the item to a new position in the new array, and then returns the new array. The given array
is not mutated.
arrayMoveMutable(array, fromIndex, toIndex)
Moves the item to the new position in the array
array. Useful for huge arrays where absolute performance is needed.
array
Type: Array
fromIndex
Type: number
The index of item to move.
If negative, it will begin that many elements from the end.
toIndex
Type: number
The index of where to move the item.
If negative, it will begin that many elements from the end.