0

I have two arrays:

arr = ["test", "hello", "year"]

arr_hashes = [{"value": "yes", "key":"test"},{"value": "no", "key":"hello"},{"value": "noway", "key":"yessir"},{"value": "never", "key":"year"}]

I want to return:

new_arr = [{"value": "yes", "key":"test"},{"value": "no", "key":"hello"},{"value": "never", "key":"year"}]

What I have no which is not filtering at all:

arr_hashes.select {|x| x['key'].include?(arr)}
0

1 Answer 1

1

It's simply the other way around arr.include?(x['key'])

so

arr_hashes.select { |x| arr.include?(x['key']) }

the object which receives the message, include?, is an array, and the parameter is the element you want to know whether it's included or not in the array

Sign up to request clarification or add additional context in comments.

2 Comments

This is still returning the original array value. How can I actually return the new set of hashes?
This returns the items for which the condition in the select is true. If you got the same array this condition is not working

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.