0
I came across a problem: I have data from a database that is related to each other so that each field has two others - one that refers to the previous field (in the table) and the other to the next field.
Something more or less like this:
... demais campos
id
prev_id
next_id
I need to draw these objects that come from the database somehow. I tried using the method .sort() Javascript, but no success:
const positions = [
{ name: 'Segundo', id: 2, prev: 1, next: 3 },
{ name: 'Quarto', id: 4, prev: 3, next: 4 },
{ name: 'Primeiro', id: 1, prev: 1, next: 2 },
{ name: 'Terceiro', id: 3, prev: 2, next: 3 }
]
/**
* Deve ficar assim:
*
* { name: 'Primeiro', id: 1, prev: 1, next: 2 },
* { name: 'Segundo', id: 2, prev: 1, next: 3 },
* { name: 'Terceiro', id: 3, prev: 2, next: 3 },
* { name: 'Quarto', id: 4, prev: 3, next: 4 }
*/
const newArray = positions.sort((a, b) => {
if (a.prev === b.id) return 1
if (a.next === b.id) return -1
return 0
})
console.log(newArray)
I assembled a small example above using a array and, in a comment, the array that needs to be achieved through ordination.
It is important to note that as the data comes from a database, I cannot necessarily assume that the Ids will be in order.
Another example to illustrate this situation:
const positions = [
{ name: 'Quarto', id: 3, prev: 1, next: 3 },
{ name: 'Primeiro', id: 5, prev: 5, next: 7 },
{ name: 'Segundo', id: 7, prev: 5, next: 1 },
{ name: 'Terceiro', id: 1, prev: 7, next: 3 }
]
// Deve ficar:
const sorted = [
{ name: 'Primeiro', id: 5, prev: 5, next: 7 },
{ name: 'Segundo', id: 7, prev: 5, next: 1 },
{ name: 'Terceiro', id: 1, prev: 7, next: 3 },
{ name: 'Quarto', id: 3, prev: 1, next: 3 }
]
Another addendum is that if the item is first on the list, it gets its own ID in the field prev. Something similar happens with the latter, which receives its own ID in the field next.
Hello Luiz! Could you give us feedback? None of the answers answered you? We can debate and come up with a solution that meets your purpose if you want. Abs!
– Sam