I have an array that contains version numbers. They are sorted, but sparse. An example could look like this:
const versions = [
{ version: 1, data: { ... }},
{ version: 3, data: { ... }},
{ version: 17, data: { ... }},
{ version: 95, data: { ... }}
];
Now if I want to access version 17, things are easy:
const data = versions.find(item => item.version === 17);
But what if I want to access version 16, e.g.? From the domain point of view what I would like to get is the highest version that was released before the desired version, in this case version 3.
If we combine both queries, this basically means that I am looking for the last entry in the array that is <= the desired version. Now the problem is: How to do that efficiently.
Things would be really easy if there was a findRight function on arrays, because then I could write:
const data = version.findRight(item => item.version <= 16);
This would return version 3. If we ran the same command with 17, it would return version 17. The only problem here is: There is no findRight function in JavaScript
I can think of two possible workarounds:
- First,
reversethe array, and then use the normalfindfunction. This unfortunately becomes quite slow, as I need to run this query lots of times in a loop for different version arrays. - Second, implement a custom
findRightfunction. This would work, but this sounds like solving a problem that has already been solved by someone else.
Before I go to implement my own function – is there a better (i.e. faster) approach that works with pure JavaScript and without npm modules?