I would like some algorithm in javascript that does the following operation:
example: I have the following array:
Entry: [1,2,3,4,5,6,7]
I want the selected items (2,3,6) to descend one position each in the array.
Expected output: [2,3,1,4,6,5,7]
How to do this algorithm for group handling of selected items.
I tried using a javascript algorithm that updates every item to the index -1 position but it does not work for group handling.
Array.prototype.move = function (old_index, new_index) {
if (new_index >= this.length) {
var k = new_index - this.length;
while ((k--) + 1) {
this.push(undefined);
}
}
this.splice(new_index, 0, this.splice(old_index, 1)[0]);
return this; // for testing purposes
};