“Elemen Pindah JavaScript di Array” Kode Jawaban

Elemen Pindah JavaScript di Array

function arrayMove(arr, fromIndex, toIndex) {
    var element = arr[fromIndex];
    arr.splice(fromIndex, 1);
    arr.splice(toIndex, 0, element);
}
Sironicas

Memindahkan indeks item dari indeks ke indeks lain, javascript

function arraymove(arr, fromIndex, toIndex) {
    var element = arr[fromIndex];
    arr.splice(fromIndex, 1);
    arr.splice(toIndex, 0, element);
}
Fragile Falcon

Elemen Pindah Array JavaScript

// Move element '3' to where currently '1' is
const numbers = [ 1, 2, 3, 4, 5 ]
const numbersOriginal = Object.assign(numbers)
const sourceIndex = 2
const targetIndex = 0
numbers.splice(targetIndex, 0, numbers.splice(sourceIndex, 1)[0])
console.log(numbersOriginal)
console.log(numbers)
Homely Hare

cara memindahkan elemen array di javascript

function moveElement(array,initialIndex,finalIndex) {
	array.splice(finalIndex,0,array.splice(initialIndex,1)[0])
	console.log(array);
	return array;
}
// Coded By Bilal
Smoggy Seal

JavaScript memindahkan item dalam array ke indeks lain

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"]
Grepper

Jawaban yang mirip dengan “Elemen Pindah JavaScript di Array”

Pertanyaan yang mirip dengan “Elemen Pindah JavaScript di Array”

Lebih banyak jawaban terkait untuk “Elemen Pindah JavaScript di Array” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya