Say I have the following array of persons:
const array = [{name: 'Glenn'}, {name: 'Rob'}, {name: 'Ronald'}]
And I want to find a certain person in this array, and put it as the first index in the array if it exist, like so:
// Rob is now first index
const array = [{name: 'Rob'}, {name: 'Glenn'}, {name: 'Ronald'}]
How can I achieve this? I can think of all kind of things, like using a find
method and save the record if it exists and then removing it from the array and use the unshift
method to set it as first index but this sounds kinda complex to me.
Preferably a ES6+ solution, and if the item doesn't exist I want to do nothing.