How to Get Particular Objects in Array?

date

Published in December 21st, 2014

Finding particular rows from a table from relational database is commonly used by many developers in developing their application. But, how to find particular objects in array. In my opinion, finding rows from a table on relational database is easier than find particular objects from array which too logical sometimes (haha). I just have to create the query that select particular rows or using active record to fetch the rows.

Why do I share this? Because for the first time I had a test for Rails job recently that forced me to use this method. Let's see how is the method works.

I have cupcakes variable.

cupcakes = [{
  "name" => "Classic Vanilla",
  "taste" => ["sweet", "vanilla"],
  "orders" => { "min" => 1, "max" => 20},
  "price" => 5.4
},
{
  "name" => "Classic Vanilla Chocolate",
  "taste" => ["really sweet", "vanilla", "chocolate"],
  "orders" => { "min" => 1, "max" => 1},
  "price" => 5.8
},
{
  "name" => "Ghirardelli Square",
  "taste" => ["sweet", "chocolate"],
  "orders" => { "min" => 1, "max" => 10},
  "price" => 5.5
},
{
  "name" => "Durian Square",
  "taste" => ["little bit salty", "sweet", "fantastic"],
  "orders" => { "min" => 5, "max" => 5},
  "price" => 7
},
{
  "name" => "Rambutan Unique",
  "taste" => ["sweet", "fantastic", "awesome"],
  "orders" => { "min" => 5, "max" => 5},
  "price" => 10
}]

I want to select cupcakes that match this following criteria,

  1. Cupcake with the most expensive and most bargain price.
  2. Cupcakes that have taste of sweet and chocolate.
  3. Cupcake that can be ordered with with specific value (eg. minimum order is 5 and the maximum is equal to the minimum order)

These are the answers.

cupcakes.minmax_by {|x| x['price']}
=> [{"name" => "Classic Vanilla",
  "taste" => ["sweet", "vanilla"],
  "orders" => { "min" => 1, "max" => 20},
  "price" => 5.4
},
{
  "name" => "Rambutan Unique",
  "taste" => ["sweet", "fantastic", "awesome"],
  "orders" => { "min" => 5, "max" => 5},
  "price" => 10 }]

Here is the first code. I use select and include? method.

cupcakes.select { |x| x['taste'].include?('sweet') && x['taste'].include?("chocolate") }
=> [{
      "name"=>"Ghirardelli Square",
      "taste"=>["sweet", "chocolate"],
      "orders"=>{"min"=>1, "max"=>10},
      "price"=>5.5
   }]

The second method is using any? (enumerable) and select.

cupcakes.select { |x| x['taste'].any? { |w| w.include?("sweet")} &&
  x['taste'].any? { |w| w.include?("chocolate") }}
  => [{
        "name"=>"Classic Vanilla Chocolate",
        "taste"=>["really sweet", "vanilla", "chocolate"],
        "orders"=>{"min"=>1, "max"=>1},
        "price"=>5.8
      },
      {
        "name"=>"Ghirardelli Square",
        "taste"=>["sweet", "chocolate"],
        "orders"=>{"min"=>1, "max"=>10},
        "price"=>5.5
      }]
cupcakes.select { |x| x['orders']['min'] == x['orders']['max'] }
=> [{
       "name"=>"Classic Vanilla Chocolate",
       "taste"=>["really sweet", "vanilla", "chocolate"],
       "orders"=>{"min"=>1, "max"=>1},
       "price"=>5.8
    },
    {
       "name"=>"Durian Square",
       "taste"=>["little bit salty", "sweet", "fantastic"],
       "orders"=>{"min"=>5, "max"=>5},
       "price"=>7
    },
    {
       "name"=>"Rambutan Unique",
       "taste"=>["sweet", "fantastic", "awesome"],
       "orders"=>{"min"=>5, "max"=>5},
       "price"=>10
    }]

That's all from me. Thanks for reading! Happy Coding!