Ruby on rails 使用地图选择几个关键点

Ruby on rails 使用地图选择几个关键点,ruby-on-rails,ruby,loops,Ruby On Rails,Ruby,Loops,我有一个包含man值的数组。我正在尝试使用另一个数组使用map方法,我想从第一个数组中选择几个值: 我的第一个数组是: data = [ "Agriculture, forestry and fishing", "Crops", "Livestock", "Forestry and logging", "Fishing and aquaculture", "Mining and quarrying", ]

我有一个包含man值的数组。我正在尝试使用另一个数组使用map方法,我想从第一个数组中选择几个值:

我的第一个数组是:

    data = [
      "Agriculture, forestry and fishing",
      "Crops",
      "Livestock",
      "Forestry and logging",
      "Fishing and aquaculture",
      "Mining and quarrying",
    ]
 b =  [
        {
        id: 1,
        Sector: "Agriculture, forestry and fishing",
        2011-12: 25.7,
        2012-13: 27,
        2013-14: 22.8,
        2014-15: 22,
        2015-16: 21.1,
        2016-17: 20.6,
        created_at: "2018-08-23T06:48:27.000Z",
        updated_at: "2018-08-23T06:48:27.000Z"
        },
        {
        id: 2,
        Sector: "Crops",
        2011-12: 17.6,
        2012-13: 18.9,
        2013-14: 14.2,
        2014-15: 13.1,
        2015-16: 12.3,
        2016-17: 12.1,
        created_at: "2018-08-23T06:48:27.000Z",
        updated_at: "2018-08-23T06:48:27.000Z"
        } 

        ....

    ]
我的第二个数组是:

    data = [
      "Agriculture, forestry and fishing",
      "Crops",
      "Livestock",
      "Forestry and logging",
      "Fishing and aquaculture",
      "Mining and quarrying",
    ]
 b =  [
        {
        id: 1,
        Sector: "Agriculture, forestry and fishing",
        2011-12: 25.7,
        2012-13: 27,
        2013-14: 22.8,
        2014-15: 22,
        2015-16: 21.1,
        2016-17: 20.6,
        created_at: "2018-08-23T06:48:27.000Z",
        updated_at: "2018-08-23T06:48:27.000Z"
        },
        {
        id: 2,
        Sector: "Crops",
        2011-12: 17.6,
        2012-13: 18.9,
        2013-14: 14.2,
        2014-15: 13.1,
        2015-16: 12.3,
        2016-17: 12.1,
        created_at: "2018-08-23T06:48:27.000Z",
        updated_at: "2018-08-23T06:48:27.000Z"
        } 

        ....

    ]
我的完整数据您可以在此处看到:

我正在使用以下代码:

   @hash_data = data.map{ |vegetable| 
          dataset = vegetable.to_s.gsub("_"," ")
          {
          type: views,
          legendText: dataset,
          showInLegend: true,
          dataPoints: b.map { |value|
          { y: value[_year], label: value[:Sector] }
          }
          }
        }

如何从b数组中选择值并仅显示数据数组中的值。

使用
Regexp.union
传递数据属性将返回一个Regexp

/Agriculture,\ forestry\ and\ fishing|Crops|Livestock|Forestry\ and\ logging|Fishing\ and\ aquaculture|Mining\ and\ quarrying/
可用于检查
b
上元素中的每个
部分
键:

b = [...]
data = [
  "Agriculture, forestry and fishing",
  "Crops",
  "Livestock",
  "Forestry and logging",
  "Fishing and aquaculture",
  "Mining and quarrying",
]
regex = Regexp.union(data)

result = b.select { |hash| hash[:Sector] =~ regex }
p result
# [{:id=>1, :Sector=>"Agriculture, forestry and fishing", :"2011-12"=>25.7, :"2012-13"=>27, :"2013-14"=>22.8, :"2014-15"=>22, :"2015-16"=>21.1, :"2016-17"=>20.6, :created_at=>"2018-08-23T06:48:27.000Z", :updated_at=>"2018-08-23T06:48:27.000Z"}, {:id=>2, :Sector=>"Crops", :"2011-12"=>17.6, :"2012-13"=>18.9, :"2013-14"=>14.2, :"2014-15"=>13.1, :"2015-16"=>12.3, :"2016-17"=>12.1, :created_at=>"2018-08-23T06:48:27.000Z", :updated_at=>"2018-08-23T06:48:27.000Z"}, ...]

p result.map { |hash| hash[:Sector] }.sort
# ["Agriculture, forestry and fishing", "Crops", "Fishing and aquaculture", "Forestry and logging", "Livestock", "Mining and quarrying"]
请注意,这对
b
上的每个值都起作用,如下所示:

{
  id: 1,
  Sector: "Agriculture, forestry and fishing",
  "2011-12": 25.7,
  "2012-13": 27,
  "2013-14": 22.8,
  "2014-15": 22,
  "2015-16": 21.1,
  "2016-17": 20.6,
  created_at: "2018-08-23T06:48:27.000Z",
  updated_at: "2018-08-23T06:48:27.000Z"
}

您可以在不使用Regexp的情况下进行尝试:

result = b.select{|e| data.include?(e[:Sector])}
p result
#=> [{:id=>1, :Sector=>"Agriculture, forestry and fishing", :"2011-12"=>25.7, :"2012-13"=>27, :"2013-14"=>22.8, :"2014-15"=>22, :"2015-16"=>21.1, :"2016-17"=>20.6, :created_at=>"2018-08-23T06:48:27.000Z", :updated_at=>"2018-08-23T06:48:27.000Z"}, {:id=>2, :Sector=>"Crops", :"2011-12"=>17.6, :"2012-13"=>18.9, :"2013-14"=>14.2, :"2014-15"=>13.1, :"2015-16"=>12.3, :"2016-17"=>12.1, :created_at=>"2018-08-23T06:48:27.000Z", :updated_at=>"2018-08-23T06:48:27.000Z"}, {:id=>3, :Sector=>"Livestock", :"2011-12"=>5, :"2012-13"=>5, :"2013-14"=>5.4, :"2014-15"=>5.7, :"2015-16"=>5.6, :"2016-17"=>5.5, :created_at=>"2018-08-23T06:48:27.000Z", :updated_at=>"2018-08-23T06:48:27.000Z"}, {:id=>4, :Sector=>"Forestry and logging", :"2011-12"=>1.7, :"2012-13"=>1.7, :"2013-14"=>1.7, :"2014-15"=>1.6, :"2015-16"=>1.5, :"2016-17"=>1.4, :created_at=>"2018-08-23T06:48:27.000Z", :updated_at=>"2018-08-23T06:48:27.000Z"}, {:id=>5, :Sector=>"Fishing and aquaculture", :"2011-12"=>1.3, :"2012-13"=>1.5, :"2013-14"=>1.6, :"2014-15"=>1.7, :"2015-16"=>1.7, :"2016-17"=>1.5, :created_at=>"2018-08-23T06:48:27.000Z", :updated_at=>"2018-08-23T06:48:27.000Z"}, {:id=>6, :Sector=>"Mining and quarrying", :"2011-12"=>0.1, :"2012-13"=>0.1, :"2013-14"=>0.5, :"2014-15"=>0.2, :"2015-16"=>0.6, :"2016-17"=>0.5, :created_at=>"2018-08-23T06:48:27.000Z", :updated_at=>"2018-08-23T06:48:27.000Z"}]

这是散列
b
的真实结构吗?您是否将“年-日”作为键(2011-12)?\u year=“2011-12”哈希的格式无效。添加一个正确的。这是错误的吗?什么是
视图
,什么是
\u年
。此外,我还假设数据点也有一个
x
值?我们可以使用b.select{{hash | hash[:Sector]=~Regexp.union(data)}.map这样的映射吗?我不确定最终的
.map
有什么用。你能解释一下吗?
data.include?(hash[:Sector])
可能更有效,因为字符串正则表达式的比较速度非常慢。另外,如果您打算坚持使用
Regexp
,那么您可能需要预构建
Regexp,因为现在您正在为
b
中的每个元素构建一个新的,您完全正确@enginersmnky,忘了将Regexp移到块外(非常尴尬)+1对于
include?
方法,似乎运行速度快了4+(Ruby 2.5.0p0)。