skip to Main Content

I have a JSON

a = {
    cake: ["a","b","c","d"]
}

I need to query any value in a given values are present in the array
Like if the input is ["a","x"] output should be true

if input is ["c","d"] then output is true

if input is ["x","z"] the n output is false

2

Answers


  1. Use intersection (&) and check if any of input is in the original array:

    2.6.6 :002 > (a[:cake] & ['a','x']).any?
     => true 
    
    2.6.6 :003 > (a[:cake] & ['x','z']).any?
     => false 
    
    Login or Signup to reply.
  2. simply try to find common between json array and input array.

    (a[:cake] & input_array).any?
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search