My task involves rearranging objects within an array.
Let's assume this is the data array provided:
const data = [
{ id: 'ETHUVMY0m', name: 'item 1', value: 'value 1' },
{ id: 'McfTB40vO', name: 'item 2', value: 'value 2' }
]
There is also another array that signifies the new order:
const order = [ 'McfTB40vO', 'ETHUVMY0m' ]
In the new order, the second item needs to be moved to the first position.
Therefore, the expected outcome is as follows:
[
{ id: 'McfTB40vO', name: 'item 2', value: 'value 2' },
{ id: 'ETHUVMY0m', name: 'item 1', value: 'value 1' }
]
One approach I considered is utilizing a forEach loop:
data.forEach(d => {
order.indexOf(d.id) // obtain the new index
// However, I am uncertain about how to reorder the array.
})