我有一个数组,该数组在包含created_at和值的每一行中包含一个哈希。如何从数组中获取值字段的最小值和最大值?
该数组称为-import * as express from "express";
test("foo", () => {
jest.mock("express", () => {
Router: () => jest.fn()
});
// ...test stuff
});
和
channels_counts_for_history_graph
给我最大日期而不是最大值吗?
channels_counts_for_history_graph.max[1]
任何帮助表示赞赏。
谢谢
答案 0 :(得分:3)
我建议使用Enumerable#minmax_by
来通过一个方法调用来获得最小值和:
array = [['Sun, 30 Dec 2018 15:03:55 UTC +00:00', 4305],['Sun, 30 Dec 2018 15:05:42 UTC +00:00', 4305],['Mon, 31 Dec 2018 09:24:06 UTC +00:00', 4306],['Sat, 05 Jan 2019 09:04:50 UTC +00:00', 4308],['Tue, 01 Jan 2019 11:26:04 UTC +00:00', 4306],['Wed, 02 Jan 2019 17:24:19 UTC +00:00', 4305]]
array.minmax_by(&:last)
#=> [["Sun, 30 Dec 2018 15:03:55 UTC +00:00", 4305], ["Sat, 05 Jan 2019 09:04:50 UTC +00:00", 4308]]
答案 1 :(得分:0)
我可能想这就是您要的:
[{ created_at: Date.new(2017, 1, 1) }, { created_at: Date.new(2019, 1, 1) }, { created_at: Date.new(2018, 1, 1) }]
.minmax_by { |value| value[:created_at] }
答案 2 :(得分:0)
默认情况下,对数组进行排序时,将首先按第一个元素进行排序。
您可以出于排序目的反转数组。
channel_counts_for_history_graph.map(&:reverse).max[0]