move-element
v0.1.0
Published
Move an array element to a given position.
Downloads
6
Readme
move-element
Move an array element to a given position.
Install
npm install --save --save-exact move-element
Usage
const moveElement = require('move-element')
// Say you have an array of fruits
const fruits = ['Oranges', 'Apples', 'Bananas', 'Pineapples']
// And you want to swap the positions of 'Apples' and 'Bananas'
// First you make a function that returns true for the element you want to move
const isApples = (x) => x === 'Apples'
// Then you call moveElement with the index you want to move the element to
const newArray = moveElement(fruits, isApple, 2)
// Then newArray will be set to:
// ['Oranges', 'Bananas', 'Apples', 'Pineapples']
License
MIT © Thomas Marek