I need to reorder some objects in an array.
Assume this is my data array:
const data = [
{ id: 'ETHUVMY0m', name: 'item 1', value: 'value 1' },
{ id: 'McfTB40vO', name: 'item 2', value: 'value 2' }
]
And there is another array, which represents the new order:
const order = [ 'McfTB40vO', 'ETHUVMY0m' ]
As you can see, the second item gets on the first place.
So the result should be:
[
{ id: 'McfTB40vO', name: 'item 2', value: 'value 2' },
{ id: 'ETHUVMY0m', name: 'item 1', value: 'value 1' }
]
I thought of using an forEach loop:
data.forEach(d => {
order.indexOf(d.id) // get new index
// but how to reorder the array?
})
sortand pass the custom function to sort