使用地图选择几把钥匙

时间:2018-08-23 10:11:50

标签: ruby-on-rails ruby loops

我有一个包含人工值的数组,我正在尝试使用另一个数组使用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"
        } 

        ....

    ]

我的完整数据可以在这里查看:My second data

我正在使用以下代码:

   @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数组中选择值,并仅显示数据数组中的值。

2 个答案:

答案 0 :(得分:2)

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

/Agriculture,\ forestry\ and\ fishing|Crops|Livestock|Forestry\ and\ logging|Fishing\ and\ aquaculture|Mining\ and\ quarrying/

可用于检查Section上元素中的每个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"
}

答案 1 :(得分:1)

您可以尝试不使用 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"}]