I'm trying to rearrange sorting of an array. Let's say I have the following structure
const array = [{
location: 'Table 2',
data: {..}
}, {
location: 'Unassigned',
data: {..}
}, {
location: 'Table 1',
data: {..}
}
];
What's the proper way of moving 'Table 1' to index 0, 'Table 2' right after it (keep same order for Table 3, 4, etc), and 'Unassigned' always to the end. Preferably with lodash.
Here's what I tried so far
forEach(allItemsSorted, (item, index) => {
const total = allItemsSorted.length;
let hasUnassigned = false;
if (item.location === 'Unassigned') {
allItemsSorted[total] = item;
hasUnassigned = true;
}
if (hasUnassigned && index === total) {
return;
}
allItemsSorted[index] = item;
})