我有JSON
a = { cake: ["a","b","c","d"] }
我需要查询任意一个值在给定的数组中是否存在值Like如果输入是["a","x"]输出应该是true
["a","x"]
true
if input is ["c","d"] then output is true if input is ["x","z"] the n output is false
dtcbnfnu1#
使用交集(&)并检查原始数组中是否有任何输入:
2.6.6 :002 > (a[:cake] & ['a','x']).any? => true 2.6.6 :003 > (a[:cake] & ['x','z']).any? => false
yb3bgrhw2#
只需尝试找到json数组和input数组之间公共点。
(a[:cake] & input_array).any?
2条答案
按热度按时间dtcbnfnu1#
使用交集(&)并检查原始数组中是否有任何输入:
yb3bgrhw2#
只需尝试找到json数组和input数组之间公共点。