I have two arrays of objects - one containing selected data and the other containing general data that needs to be displayed
General data for display
const arr = [
{
id: "1",
name: "Skoda - Auto"
},
{
id: "2",
name: "BMW - Auto"
},
{
id: "3",
name: "Mustang"
},
{
id: "2",
name: "Ferrari"
},
{
id: "1",
name: "Ford"
}
];
selectedValues
const selectedArr = [
{
id: "1",
name: "something - 1"
},
{
id: "3",
name: "something - 1"
}
]
I need to rearrange the general data array based on the selected values array. The goal is to prioritize the items in the general array that match the ids in the selected array and move them to the top. Expected Output:
const arr = [
{
id: "1",
name: "Skoda - Auto"
},
{
id: "1",
name: "Ford"
},
{
id: "3",
name: "Mustang"
},
{
id: "2",
name: "BMW - Auto"
},
{
id: "2",
name: "Ferrari"
},
];
There may be multiple values with the same id, which should all be moved to the top if they exist in the selected array. I am not sure how to achieve this, so any help would be greatly appreciated.