shift array from index javascript code example
Example 1: array shift javascript
const array1 = [1, 2, 3];
const firstElement = array1.shift();
console.log(array1);
console.log(firstElement);
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));