1, "timestamp" => 1383314276, "data" => "bea-6ren">
gpt4 book ai didi

ruby - 从一个散列元素具有最高值的数组中选择单个散列

转载 作者:数据小太阳 更新时间:2023-10-29 08:32:21 24 4
gpt4 key购买 nike

从以下哈希数组中,如何选择 timestamp 具有最高值的单个哈希?

data = [
{"id" => 1, "timestamp" => 1383314276, "data" => "bea7c82f-f4b2-492a-aba3-033b1a54d9d0"},
{"id" => 2, "timestamp" => 1383314386, "data" => "64ed2ed9-763d-443f-a74e-e7f10cbe783e"},
{"id" => 3, "timestamp" => 1383314331, "data" => "f8cfaa99-ffe0-4d88-8fac-37e4ce462d3a"}
]

最佳答案

在这里你可以看到:

 data.max_by{|e| e["timestamp"] }
# >> {"id"=>2, "timestamp"=>1383314386, "data"=>"64ed2ed9-763d-443f-a74e-e7f10cbe783e"}

关于ruby - 从一个散列元素具有最高值的数组中选择单个散列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19728461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com