I have objects that have such fields id
and position
.
const items = [{id: 11, position: 1}, {id: 12, position: 2}, {id: 13, position: 3}, {id: 14, position: 4}, {id: 15, position: 5}, {id: 16, position: 6}];
These items are basically folders stacked one on top of another, it's possible to move these folders relative to each other which means changing their position property.
I need a function like this:
const moveDir = (idIn: number, idOut: number, currentList: Dir[]): Dir[] => {
// some code;
return;
}
that would return a new list of folders after the change has taken place. As for the idIn
and idOut
params: I want to drag a folder with id idIn
to the position of a folder with id idOut
.
Example:
--------
Folder 1
--------
Folder 2
--------
Folder 3
--------
Folder 4
--------
Folder 5
--------
If idIn = 2
, idOut = 4
, the result should be:
--------
Folder 1
--------
Folder 3
--------
Folder 4
--------
Folder 2
--------
Folder 5
--------
If idIn = 4
, idOut = 2
, the result should be:
--------
Folder 1
--------
Folder 4
--------
Folder 2
--------
Folder 3
--------
Folder 5
--------
Any ideas on how to implement that behaviour would be helpful.
EDIT:
The initial list of objects looks like this:
const items = [{id: 11, position: 1}, {id: 12, position: 2}, {id: 13, position: 3}, {id: 14, position: 4}, {id: 15, position: 5}];
I know items ids and if I want to change two of them, I should be able to pass the ids of those items the positions of which I want to change.
The edited example:
--------
Folder 1 (id = 11, position = 1)
--------
Folder 2 (id = 12, position = 2)
--------
Folder 3 (id = 13, position = 3)
--------
Folder 4 (id = 14, position = 4)
--------
Folder 5 (id = 15, position = 5)
--------
If I change Folder 2 and Folder 4, it means idIn = 12
and idOut = 14
.
The result should be:
--------
Folder 1 (id = 11, position = 1)
--------
Folder 3 (id = 13, position = 2)
--------
Folder 4 (id = 14, position = 3)
--------
Folder 2 (id = 12, position = 4)
--------
Folder 5 (id = 15, position = 5)
--------
That's the function should return this list (ordered by position):
[{id: 11, position: 1}, {id: 13, position: 2}, {id: 14, position: 3}, {id: 12, position: 4}, {id: 15, position: 5}];