0

How in javaScript from this array find the maximum number

[ [ 'a' , 0.01 ] , [ 'b' , 0.02 ] , ...] 

I need to find the maximum number of 0.01 , 0.02 ...

How in javaScript from the above array

make this array

 [ 0.01 , 0.02 , ...]

and using the function Math.max() find the maximum number?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 3
    Welcome to Stack Overflow! Please visit the [help], take the [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output using the `[<>]` snippet editor. – mplungjan Nov 12 '20 at 14:59
  • 2
    `map()`, then `Math.max()` – Taplar Nov 12 '20 at 14:59
  • 2
    try `Math.max(...array.map(([_, n]) => n))` – Nenad Vracar Nov 12 '20 at 14:59
  • You always have 2 values in the sub arrays and the second one is always a number? – Cid Nov 12 '20 at 15:01
  • [Many more dupes](https://www.google.com/search?q=javascript+math.max+array+site:stackoverflow.com) – mplungjan Nov 12 '20 at 15:09

2 Answers2

1

You can map the value by destructuring the key-value pair and returning the value. After you get the values, you can spread them in the Math.max call.

const arr = [ [ 'a' , 0.01 ] , [ 'b' , 0.02 ] ];

console.log(Math.max(...arr.map(([k, v]) => v)));
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0

You can do something like this :

array = [['a' , 0.01] , ['b' , 0.02] , ...]
    
const numberArray = array.map(([key, value]) => value)

And then use the Math.max() method.