Answers for "how to move an element of an array in javascript"

0

javascript move element in array

function arrayMove(arr, fromIndex, toIndex) {
    var element = arr[fromIndex];
    arr.splice(fromIndex, 1);
    arr.splice(toIndex, 0, element);
}
Posted by: Guest on March-16-2021
2

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);
}
Posted by: Guest on June-07-2020
1

how to move an element of an array in javascript

function moveElement(array,initialIndex,finalIndex) {
	array.splice(finalIndex,0,array.splice(initialIndex,1)[0])
	console.log(array);
	return array;
}
// Coded By Bilal
Posted by: Guest on September-21-2020
0

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; 
};

//move index 1(b) to index 2(c)
console.log(moveArrayItemToNewIndex(["a","b","c","d"], 1, 2)); // returns ["a", "c", "b", "d"]
Posted by: Guest on August-02-2019

Code answers related to "how to move an element of an array in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language