Reputation: 9407
I would like to pick an element in an array, move it to another index, and then "shift/rotate" the "in-between" elements by 1.
Imagine a drag and drop functionality, if the index from
is less than index to
, then I would like to shift to be to the left, and else shift to be to the right.
Input:
let data = [ 0, 1, 2, 3, 4, 5, 6 ]
Task1:
insertAndShift(data, 0, 3): Take element at index 0, shift indexes 1, 2, 3 to the left, and then insert index 0 at 3
Expected Output:
[ 1, 2, 3, 0, 4, 5, 6 ]
Task2:
insertAndShift(data, 3, 0) Take element at index 3, shift indexes 0, 1, 2 to the right and then insert index 3 at 0
Expected Output:
[ 0, 1, 2, 3, 4, 5, 6 ]
I tried this:
Following Rotating an array in place, ugly and not working code (temp always undefined + result is not complete + possible work only for RIGHT):
insertAndShift(data, from, to)
{
if(from < to)
{
// rotate in between - LEFT
}
else
{
// swap elements
let temp = data[from];
data[from] = data[to];
data[to] = temp;
// rotate in between - RIGHT
let newData = this.rotate(data.slice(to, from - 1), 1)
let result = [];
result.push(data.slice(0,1));
newData.map((element) => result.push(element))
for(let i = from+1; i < data.length; i++)
{
result.push(data[i]);
}
this.setState({data: result})
}
}
rotate(arr, k) {
var l = arr.length;
arr = this.reverseArr(arr, 0, l - 1);
arr = this.reverseArr(arr, 0, k - 1);
arr = this.reverseArr(arr, k, l - 1);
return arr;
}
reverseArr(arr, left, right) {
while (left < right) {
var temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
return arr;
}
Also following Javascript (dynamic) insert into array, then shift all elements underneath +1, which returns only 1 item:
else
{
let result = data.splice(to, 1, data[from]);
this.setState({allTasksArray: result})
}
How can I achieve this for both Left and Right?
Upvotes: 11
Views: 26452
Reputation: 21
Take a look at this method: Array.prototype.copyWithin().
There is a more efficient in-place way to do it:
function insertAndShift(data, src, dest) {
if (src === dest)
return;
let tmp = data[src];
if (src < dest) {
data.copyWithin(src, src + 1, dest + 1);
} else /* if (src > dest) */ {
data.copyWithin(dest + 1, dest, src);
}
data[dest] = tmp;
}
let data = [0, 1, 2, 3, 4, 5, 6];
insertAndShift(data, 0, 3);
console.log("[" + data + "]");
insertAndShift(data, 3, 0);
console.log("[" + data + "]");
Below is an illustration:
Case 1 (src < dest):
[..Sabcd..]
//// // data.copyWithin(src, src+1, dest+1);
[..abcdd..]
[..abcdS..] // data[dest] = tmp;
Case 2 (src > dest):
[..abcdS..]
\\\\ // data.copyWithin(dest+1, dest, src);
[..aabcd..]
[..Sabcd..] // data[dest] = tmp;
Upvotes: 1
Reputation: 1237
The answer above works great but it's a little handwavy... if you are interested in a solution that spells out what it's doing I wrote this:
const insertAndShift = (arr: number[], to: number, from: number) => {
let newArray: number[] = [];
const fromItem = arr[from];
if (from > to) {
const startToTo = (to > 0) ? arr.slice(0, to) : [];
const toToFrom = arr.slice(to, from);
const fromToEnd = arr.slice(from + 1, arr.length);
newArray = newArray.concat(startToTo, [fromItem], toToFrom, fromToEnd);
}
if (to > from) {
const startToFrom = (from > 0) ? arr.slice(0, from) : [];
const fromToTo = arr.slice(from + 1, to + 1);
const toToEnd = arr.slice(to + 1, arr.length);
newArray = newArray.concat(startToFrom, fromToTo, fromItem, toToEnd);
}
return newArray;
};
const array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(insertAndShift(array, 9, 0));
console.log(insertAndShift(array, 5, 1));
Upvotes: 2
Reputation: 31712
You can use Array.prototype.splice
to cut out the element you want and insert at the desired index (the shifting will happen automatically):
function insertAndShift(arr, from, to) {
let cutOut = arr.splice(from, 1) [0]; // cut the element at index 'from'
arr.splice(to, 0, cutOut); // insert it at index 'to'
}
let data = [ 0, 1, 2, 3, 4, 5, 6 ];
insertAndShift(data, 0, 3);
console.log("[" + data + "]");
insertAndShift(data, 3, 0);
console.log("[" + data + "]");
Upvotes: 32