So I have an object of objects where each inside object has it is own seq property which points at it's order inside the big object.
Example:
const obj = {
'1': {
name: 'apple',
seq: 1,
id: 1,
},
'2': {
name: 'orange',
seq: 2,
id: 2,
},
'3': {
name: 'banana',
seq: 3,
id: 3,
},
}
My goal is to move these objects around and update their seq accordingly.
For example
const obj = {
'1': {
name: 'apple',
seq: 1,
id: 1,
},
'3': {
name: 'banana',
seq: 2,
id: 3,
},
'2': {
name: 'orange',
seq: 3,
id: 2,
},
}
I know the id of the object I'm moving and to which position it is moving
What I have so far
const moveObjects = (objId, indexToMoveTo) => {
const objCopy = Object.values(obj).reduce((prev, curr) => {
// if element if the one we moved - set seq to index
if (curr.id === objId) {
prev[curr.id] = {
...curr,
seq: indexToMoveTo
};
} else if (curr.id !== objId) {
// if element is not the one and index is smaller - increase seq by one
if (curr.seq >= indexToMoveTo) {
prev[curr.id] = {
...curr,
seq: curr.seq + 1
};
}
// if index bigger than seq - return the element without modifying it
else if (curr.seq < indexToMoveTo) {
prev[curr.id] = {
...curr
};
}
}
return prev;
}, {})
return objCopy
}
The problem with this is that of course if you move an element around a few times seq starts to increase beyond the length of the object, it can become 4, 5, 6 for example.
Here is the link to the jsfiddle to see more clearly what I mean. (the third console.log)
All the help to resolve this will be much appreciated