Object doesn't guantee order when I display it so I have to convert an existing object into array, but follow the order.
I have this array as the order [1,2,3]
and this is my raw input aka source
{'2': 'two',
'3': 'three',
'1':'one'}
How can I make a function to produce a new array where the order follow the sorted key above?
I'm stuck at this stage
//expected
['one', 'two', 'three']
const order = ['1', '2', '3']
const source = {
'2': 'two',
'3': 'three',
'1': 'one'
}
let sorted = Object.keys(source).map(o => {
//return order.includes(o) ? //what to do here : null
})
I think I have to do loop within loop.