adbarads adbarads - 23 days ago 4x
JSON Question

how to select elements in array from multiple elements in another array

I have an array A that looks like this:

A = [{"id"=>"1234", "name"=>"audi", "isCool"=>false},
{"id"=>"5678", "name"=>"acura", "isCool"=>false},
{"id"=>"9101112", "name"=>"bentley", "isCool"=>true},
{"id"=>"13141516", "name"=>"rollsroyce", "isCool"=>true},
{"id"=>"17181920", "name"=>"toyota", "isCool"=>true}]

and I have an array B that looks like this:

B = ["1234", "13141516”]

I am trying to select only elements from array A that match array A's ids with Array Bs elements.

So the returned results I would like is:

C = [{"id"=>"1234", "name"=>"audi", "isCool"=>false},{"id"=>"13141516", "name"=>"rollsroyce", "isCool"=>true}]

Is there an easy way to go about this?

I have currently tried this but obviously not a good idea: {|x| x['id'] == B.first || B.last}

But obviously this is not dynamic, because what if I had 3 or 4 elements in array

Answer { |x| B.include?(x['id']) }