Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby: Array of Hashes
data = [{ a: 1, b: 9 }, { a: 1, b: 8 },
{ a: 2, b: 7 }, { a: 2, b: 6 }]
data.group_by { |h| h[:a] }.transform_values { |xs| xs.max_by { |x| x[:b] } }
def key(k)
proc { |hash| hash[k] }
end
data.group_by(&key(:a)).transform_values { |xs| xs.max_by(&key(:b)) }
class Array
def to_proc
proc { |hash| hash[first] }
end
end
data.group_by(&[:a]).transform_values { |xs| xs.max_by(&[:b]) }
@junegunn

This comment has been minimized.

Copy link
Owner Author

@junegunn junegunn commented Dec 18, 2019

🤷‍♂️ 🤷‍♀️

@junegunn

This comment has been minimized.

Copy link
Owner Author

@junegunn junegunn commented Dec 18, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment