Array.include?多个值 [英] Array.include? multiple values

查看:44
本文介绍了Array.include?多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[2, 6, 13, 99, 27].include?(2) 可以很好地检查数组是否包含一个值.但是如果我想检查一个数组是否包含多个值列表中的任何一个呢?有没有比 Array.include?(a) 或 Array.include?(b) 或 Array.include?(c) ... 更短的方法?

[2, 6, 13, 99, 27].include?(2) works well for checking if the array includes one value. But what if I want to check if an array includes any one from a list of multiple values? Is there a shorter way than doing Array.include?(a) or Array.include?(b) or Array.include?(c) ...?

推荐答案

你可以取两个数组的交集,看看它是否不为空:

You could take the intersection of two arrays, and see if it's not empty:

([2, 6, 13, 99, 27] & [2, 6]).any?

这篇关于Array.include?多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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