如何在我的散列中查找键以映射到符合条件的元素? [英] How do I find keys in my hash to map to elements that match a condition?

查看:72
本文介绍了如何在我的散列中查找键以映射到符合条件的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将整数映射到数组的散列。例如

I have a hash that maps integers to arrays. For example

{1 => ["abc"], 2 => ["ccc", "ddd"]}

如何从我的散列中获得所有密钥有数组中至少有两个元素?

How do I get all the keys from my hash that have arrays with at least 2 elements in them?

推荐答案

{1 => ["abc"], 2 => ["ccc", "ddd"]}.select{|_, a| a.length > 1}.keys
# => [2]

这篇关于如何在我的散列中查找键以映射到符合条件的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆