On what I'm working right now is a little bit complex for my brain. I've got some data, and that data has a field 'position', and based on that position they are going to be displayed on the client side in that order(for example, for the last item user has added, he can change its position to be 1 and on client side it will be displayed first, then comes rest of the data), and he can always change the position(from 1 to 8, and then it will be displayed the last because that number of data is limited to maximum of 8). But the problem is when user changes the position for example from 4 to 1, and there already is a data with position 1, so then we have to items with the same position, which should not happen. Is there a solution to go over the array, and check for same values and then replace them?
Example: There are 2 items, item 1 has position 1 and item 2 has position 2. If we change item 2 position to 1, then both of them will have one, but that item 1 should automatically auto increment to 2.
What I tried so far was do forEach on the array, and check values with conditions but its not working the best. Is there some algorithm to accomplish this?
this.items.forEach((itemRes) => {
let itemDash = result;
if (itemRes.position === result.ordinal) {
if(itemRes.position !== result) {
itemRes.ordinal++;
}
} else if (itemRes.position === this.items.length && itemRes.ordinal >= 8) {
itemRes.position--;
}
})
Here is my code for checking and changing array items and their positions.