Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find in an array from right without reversing?

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, 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.
  • Second, implement a custom 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?

like image 357
Golo Roden Avatar asked Nov 15 '18 10:11

Golo Roden


1 Answers

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))
like image 199
Nitish Narang Avatar answered Nov 15 '22 01:11

Nitish Narang