I have an array that looks like this:
cPrefs = [0:{ id: 5, name: "Sixth thing" },
1:{ id: 3, name: "Fourth thing" },
2:{ id: 4, name: "Fifth thing" },
3:{ id: 0, name: "First thing" },
4:{ id: 2, name: "Third thing" },
5:{ id: 1, name: "Second thing" }]
And I have another sorting array that looks like this:
cOrder = ["1", "3", "2", "5"]
I need to sort the first array by the second (which has ids) and leave the non identified objects at the end (in any order). So a correct final sorting could look like this:
[0:{ id: 1, name: "Second thing" },
1:{ id: 3, name: "Fourth thing" },
2:{ id: 2, name: "Third thing" },
3:{ id: 5, name: "Sixth thing" },
4:{ id: 0, name: "First thing" },
5:{ id: 4, name: "Fifth thing" }]
I am not sure of the best way to accomplish this. I have tried
const output = cOrder.map(i => cPrefs[i].id)
but it throws out my other values, and I suppose I could loop through and rebuild the array, but I was curious if there was a more efficient way.