change items position within array js code example
Example 1: moving a item fro index to another index, javascript
function arraymove(arr, fromIndex, toIndex) {
var element = arr[fromIndex];
arr.splice(fromIndex, 1);
arr.splice(toIndex, 0, element);
}
Example 2: javascript move item in array to another index
function moveArrayItemToNewIndex(arr, old_index, new_index) {
if (new_index >= arr.length) {
var k = new_index - arr.length + 1;
while (k--) {
arr.push(undefined);
}
}
arr.splice(new_index, 0, arr.splice(old_index, 1)[0]);
return arr;
};
console.log(moveArrayItemToNewIndex(["a","b","c","d"], 1, 2));