2

My question is how to I pull out a specific object from this array that has an 'id' with the specific value of 888?

[{    "token" => "1212",
   "category" => "A",
       "name" => "page 2",
         "id" => "888"
 },

 {    "token" => "3434",
   "category" => "B",
       "name" => "page 1",
         "id" => "999",
 }]

I have tried find_by, where, and a whole host of other things.

steenslag
  • 79,051
  • 16
  • 138
  • 171
Verty00
  • 726
  • 5
  • 16
  • possible duplicate of http://stackoverflow.com/questions/2244915/how-do-i-search-within-an-array-of-hashes-by-hash-values-in-ruby – John Feltz Oct 09 '16 at 22:01

1 Answers1

4

You can try using "select" on the array:

arr.select {|k| k['id'] == "888" }

This will return an array containing all array elements where the condition is met.

Hawkeye001
  • 791
  • 2
  • 11
  • 25