Here’s what i’m trying to do. I have an Array arr with strickly 4 elements. can’t have more and can’t have less. I have another Array called shiftAmounts.
So here’s what I have and the result i’m looking for…
arr[A, B, C, D]
shiftAmounts[0, 1, 1, 3]
result: arr[A,"Empty", B, C]
So starting from the right (the fouth elements) it<s important, I have to move the D to the right by 3 space. Since I can’t have more than 4 elements, then it’s deleted
Next the C move one space to the right
Next the B move one space to the right
Next the A don’t move cause it has 0.
Elements never cross each others.
The change has to be in place.
tks for your help
Seby
2
Answers
i have made a function for you which can achieve you desired outcome
Map each element of the array to a list of elements and new positions.
Then, use
Object.fromEntries
to treat that array as a list of key-value pairs and create an object from them, and useArray.from
to convert that into an array. Note thatArray.from
will automatically exclude elements in positions greater than the specified array length.The second parameter to
Array.from
is an arrow function that replaces missing array elements with the string ‘Empty’.