A pure function to swap the position of two elements in the array
$ npm install pure-swap --save
- IE 9+
- Chrome 5+
- Opera 10.5+
- Safari 5+
- FireFox 4+
var pureSwap = require('pure-swap');
var array = [1,2,3,4];
pureSwap(array, 1, 3); // => [1,4,3,2]
pureSwap(array, 3, 1); // => [1,4,3,2]
// It doesn't modify the original array
console.log(array) // => [1,2,3,4]
A pure function to swap the position of two elements in the array
- Array
array
: An array of data - Number
indexToSwap
An index of array element to swap - Number
indexToBeSwapped
An index of array element to be swapped
- Array: Result
MIT © Philipp Alferov