How to find the smallest version number in an array of browser–version pairs?

Leslie Alldridge

I’m looking for alternative solutions for returning specific items from an array.

[
  ["chrome", "72"],
  ["chrome", "71"],
  ["chrome", "70"],
  ["edge", "18"],
  ["edge", "17"]
]

How would I return the oldest version of each browser, e.g. [ [ "chrome", "70" ], [ "edge", "17" ] ]?

I currently have a filter for chrome and a filter for edge, etc. But I wonder if there’s a cleaner way of achieving the same result.

Thank you

You can use reduce and min

const data = 
  [ ["chrome", "72"]
  , ["chrome", "71"]
  , ["chrome", "70"]
  , ["edge", "18"]
  , ["edge", "17"]
  ]

const result = 
  data.reduce
    ( (acc, [ browser, version ]) =>
        ({ ...acc, [browser]: Math.min(version, acc[browser] || Infinity) })
    , {}
    )
    
console.log(result)
// { "chrome": 70
// , "edge": 17
// }

console.log(Object.entries(result))
// [ [ "chrome", 70 ]
// , [ "edge", 17 ]
// ]

If you want the latest version, use max instead -

const result = 
  data.reduce
    ( (acc, [ browser, version ]) =>
        ({ ...acc, [browser]: Math.max(version, acc[browser] || 0) })
    , {}
    )

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to find the smallest number in the array without sorting it?

find the smallest number in a array of an array

find smallest number in array javascript

How to find androidx.browser:browser version?

How to find smallest numbers in an array

Find the smallest sum of the absolute differences of k pairs of an array

how to find version number of Julia? Is there a ver() command?

How do I find the openpyxl version number

How do I find the Index of the smallest number in an array in python if I have multiple smallest numbers and want both indexes?

Find the number of pairs in an array whose difference is K?

How to find the array containing the smallest value?

find the smallest number in a list

I can't find smallest number by this code. How I find smallest number by modifying this code?

How to find the *smallest* number in the list that is close to a given number in Python?

Find smallest and biggest number of an array with reduce function in javascript

Find largest and smallest number in array using pointers in C

two sorted array and find k'th smallest number

Find the average. largest, smallest number and mode of a int array

Sorting array elements to find the largest and smallest number in C

find the compiled class version number

In an input array in Java, how do I find the number of pairs of values that are equal?

How to find the number of adjacent pairs in an array that have a difference greater than "n"?

Smallest number in array

smallest number in array [C]

Smallest number in array that is not 0

How to find the version of Chrome browser from my extension?

How to find out the version number in cakephp v3.0?

How do I find out the version number of an installed library?

How can I programmatically find Bluebird's version number?