I have an algorithm where some array will be given a new index, but the new index is not sequential. For the sake of time, just assume the new index is given to the array at random. Let's say I have this array:
const arr = [];
arr[5] = 'a'
arr[2] = 'b'
arr[9] = 'c'
arr[0] = 'd'
If I console.log this array to the output window, I get this:
['d', empty, 'b', empty × 2, 'a', empty × 3, 'c']
I'm wondering if it's possible to close/remove those empty gaps between values so the array entries are sorted by their actual index, so I can get this output:
['d', 'b', 'a', 'c']
Is there a short and easy way to do this?