moveInArray.js
Move an item from one index to another in an array.
Works in all modern browsers, and at least back to IE9.
/*!
* Move an item from one index to another in an array.
* (c) 2020 Chris Ferdinandi, MIT License, https://gomakethings.com
* @param {Array} arr The array to move the item in
* @param {Integer} from The starting index of the item
* @param {Integer} to The new index for the item
*/
var moveInArray = function (arr, from, to) {
// Make sure a valid array is provided
if (Object.prototype.toString.call(arr) !== '[object Array]') {
throw new Error('Please provide a valid array');
}
// Delete the item from it's current position
var item = arr.splice(from, 1);
// Make sure there's an item to move
if (!item.length) {
throw new Error('There is no item in the array at index ' + from);
}
// Move the item to its new position
arr.splice(to, 0, item[0]);
};