我有一个数组的Array
。可以有两个或多个子数组:
array = [
["66892885", "66891801", "66924833", "66892255"],
["65167829", "65167828", "66924833"],
["66924833", "66891801"]
]
我只需要在每个子数组中找到的值。因此在这种情况下,“66924833”
将是唯一的匹配项。为了使值显示在结果中,每个子数组都必须包含它。
我该怎么做?
最佳答案
您可以组合inject与数组的 Set Intersection (#&
)像这样的方法
array.inject(:&)
获得想要的结果:
array=[["66892885", "66891801", "66924833", "66892255", "1", "33"],
["65167829", "65167828", "66924833", "1", "33", "44"],
["2344", "66924833", "1", "33"]]
array.inject(:&)
#=>["66924833", "1", "33"]
关于ruby-on-rails - 查找数组的每个子数组包含的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58384650/