I have an object with simple key value pairs and an array with just key names. I would like to sort the objects keys by the order of the keys in the array.
A simplified example:
const obj = {
"bread": 11,
"butter": 6,
"milk": 40,
}
const orderedKeys = ["milk", "butter", "bread"]
const expectedResult = {
"milk": 40,
"butter": 6,
"bread": 11,
}
In this example I know the amount of keys in both the array and the object, however in reality I don't know the amount and in reality the object can also have more keys than specified in the array. In this case I just want the unknown keys at the end of the object.
What would be the cleanest way to solve this problem?