livescriptprelude.ls

Get minimum of a object


Firstly, good morning.

PreludeLS is able to get the minimum value of an array:

[1 2 3 4 5 6 7 8 9 10] |> minimum #=> 1

Now I figure out a way to get the minimum-by a unknown key. Let's suppose I have an object called A and it has 3 properties with 3 float values:

A =
  A: 3.2
  B: 4.2
  C: 4.7

And I want to return the KeyValuePair of the element with a lower value:

{A: 32}

I can get the minimum by several objects by these objects having an equal index. How can I get the minimum by a unknown key?


Solution

  • A |> obj-to-pairs |> minimum-by (.1)
    #=> ['A', 3.2]
    
    A |> obj-to-pairs |> minimum-by (.1) |> -> {(it.0): it.1}
    #=> {A: 3.2}