Ruby on rails 如何比较多维数组元素和哈希数组

Ruby on rails 如何比较多维数组元素和哈希数组,ruby-on-rails,ruby,arrays,multidimensional-array,hash,Ruby On Rails,Ruby,Arrays,Multidimensional Array,Hash,我正在尝试对数据实现动态过滤。我有多维数组的过滤器选项: filter_option = [["accountid", "<", "2"],["first_name", "=", "John"],["lastname", "=", "deo"]] 所有筛选器选项元素都应与报表数组的每个哈希匹配。例如,在filter\u option中,我们有account\u idfilter[2]。则条件满足\u flag+=1 结束 结束 过滤的\u报告不清楚你有什么。不要在没有解释的情况下引入变量

我正在尝试对数据实现动态过滤。我有多维数组的过滤器选项:

filter_option = [["accountid", "<", "2"],["first_name", "=", "John"],["lastname", "=", "deo"]]
所有筛选器选项元素都应与
报表
数组的每个哈希匹配。例如,在
filter\u option
中,我们有
account\u id<2
first\u name=john
last\u name=deo
。所以我需要满足所有这些过滤器的散列

如果
filter\u选项
为:

filter_option = [["accountid", ">", "2"]]
那么输出应该是:

filtered_report = [{accountid: 4, first_name: "sherry", lastname: "b"},{accountid: 3, first_name: "Jimmy", lastname: "S"}]
filtered_report = [{accountid: 2, first_name: "john", lastname: "deo"}]
filtered_report = [{accountid: 4, first_name: "sherry", lastname: "b"}]
如果过滤器选项为:

filter_option = [["accountid", "=", "2"],["first_name","=","john"],["lastname","=","deo"]]
filter_option = [["accountid", ">", "3"],["first_name","=","sherry"],["lastname","=","b"]]
那么输出应该是:

filtered_report = [{accountid: 4, first_name: "sherry", lastname: "b"},{accountid: 3, first_name: "Jimmy", lastname: "S"}]
filtered_report = [{accountid: 2, first_name: "john", lastname: "deo"}]
filtered_report = [{accountid: 4, first_name: "sherry", lastname: "b"}]
如果过滤器选项为:

filter_option = [["accountid", "=", "2"],["first_name","=","john"],["lastname","=","deo"]]
filter_option = [["accountid", ">", "3"],["first_name","=","sherry"],["lastname","=","b"]]
那么输出应该是:

filtered_report = [{accountid: 4, first_name: "sherry", lastname: "b"},{accountid: 3, first_name: "Jimmy", lastname: "S"}]
filtered_report = [{accountid: 2, first_name: "john", lastname: "deo"}]
filtered_report = [{accountid: 4, first_name: "sherry", lastname: "b"}]
我没有得到我需要的解决方案。有人知道最好的解决方案是什么,以获得我需要的输出吗

我编写的代码如下:

filtered_report = []
  filter_option.each do |f|
    reports.each do |r|
      r.each do |k,v|
        if f[1] == "="
          if f[0].to_sym == k && v == f[2]  
            filtered_report << r
          end
        elsif f[1] == ">"
          if f[0].to_sym == k && v > f[2].to_i
            filtered_report << r
          end
        elsif f[1] == "<"
          if f[0].to_sym == k && v < f[2].to_i
            filtered_report << r
          end
        end
      end
    end
  end

这是不正确的,因为数组中没有一个哈希满足
filter\u option
数组中给定的所有筛选器,并且输出应该是
nil

您的代码在筛选器选项之间实现了
关系-如果其中任何一个选项对某个项正确,则该项将添加到结果中(有时不止一次……)

要实现为
,您需要确保在将某个项添加到结果中之前,该项的所有规则都已通过。实现该项的最简单方法是假设结果包含所有元素,然后删除任何未通过规则的元素:

  filtered_report = reports.dup
  filter_option.each do |f|
    filtered_report.reject! do |r|
      !r.any? do |k,v|
        if f[1] == "="
          if f[0].to_sym == k && v == f[2]  
           true
          end
        elsif f[1] == ">"
          if f[0].to_sym == k && v > f[2].to_i
            true
          end
        elsif f[1] == "<"
          if f[0].to_sym == k && v < f[2].to_i
            true
          end
        end
      end
    end
  end
上面的代码是做什么的

select
仅返回块返回的元素
true

any?
返回
true
如果块的任何运行(每个元素一次)返回
true

all?
如果块的所有运行都返回
true
,则返回'true'

send(op,value)
实际运行元素上的运算符(即
“niokie”==“John”
),并返回其值。

请尝试下面的代码

    filtered_report = []

    filter_option_length = filter_option.length
    reports.each do |report|    
        condition_satisfied_flag = 0
        filter_option.each do |filter|
            if filter[1] == "="
                condition_satisfied_flag += 1 if report[filter[0].to_sym].to_s.downcase == filter[2].downcase               
            end

            if filter[1] == "<"
                condition_satisfied_flag += 1 if report[filter[0].to_sym].to_s.downcase < filter[2].downcase
            end

            if filter[1] == ">"
                condition_satisfied_flag += 1 if report[filter[0].to_sym].to_s.downcase > filter[2].downcase
            end 
        end
        filtered_report << report if condition_satisfied_flag == filter_option_length
    end
filtered_报告=[]
filter\u option\u length=filter\u option.length
报告。每个人都做报告
条件满足\u标志=0
过滤器|选项。每个do |过滤器|
如果过滤器[1]=“=”
如果报告[filter[0]。to[u sym]。to[u s.downcase==filter[2]。downcase,则满足条件[u]
结束
如果筛选器[1]==“”
如果报告[filter[0]。到[u sym]。到[u s.downcase>filter[2]。则条件满足\u flag+=1
结束
结束

过滤的\u报告不清楚你有什么。不要在没有解释的情况下引入变量/方法。过滤选项与
filter\u选项
有何不同?
报告
数组与
报告
数组有何不同?不清楚为什么输出应该是
nil
,而不是你上一次解释中的空数组。@sawa i下次提问时请记住,谢谢你的建议。是的,输出应该是空数组而不是空数组。在匹配条件下,你的方法是正确的&而不是| |,我也会按照你的方法来获得解决方案。谢谢。你的回答完全正确,我需要你的帮助。谢谢你的帮助。