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:
reverse
the array, and then use the normal find
function. This unfortunately becomes quite slow, as I need to run this query lots of times in a loop for different version arrays.findRight
function. 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?
You can also use "reduceRight" for this like below
const versions = [
{ version: 1, data: 1},
{ version: 3, data: 3},
{ version: 17, data: 17},
{ version: 95, data: 95}
];
function getNextVersion(v) {
return versions.slice(0).reduceRight((a , b, i , arr) => b.version <= v ? (arr.length = 0, b) : b)
}
console.log(getNextVersion(16))
console.log(getNextVersion(17))
console.log(getNextVersion(18))
console.log(getNextVersion(2))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With