how to move an element of an array in javascript
function arraymove(arr, fromIndex, toIndex) {
var element = arr[fromIndex];
arr.splice(fromIndex, 1);
arr.splice(toIndex, 0, element);
}function moveElement(array,initialIndex,finalIndex) {
array.splice(finalIndex,0,array.splice(initialIndex,1)[0])
console.log(array);
return array;
}
// Coded By Bilalfunction 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"]
Also in JavaScript:
- callout react
- fizzbuzz js
- recuperer une image dans la base de données avec angular
- node js get files in dir
- javascript how to do else if
- js replace blogger thumb
- node test unit
- adding numbers in an array javascript
- javascript sort array of objects by key value ascending and descending order
- dom is loaded
- onclick inline function react
- javascript get hour from date
- string charat javascript
- string concatenation in js
- how to remove name in react navigation header
- port 3000 is already in use nodemon app crashed
- jquery script url
- javascript is int in array
- javascript class in external file
- Javascript looping through table
- jquery get version
- email validation in javascript
- javascript remove quotes from string
- js opposite of preventdefault