How to Filter an Array of Hashes to Get Only the Keys in Another Array

How can I filter an array of hashes to get only the keys in another array?

This should do what you want:

events.map do |hash|
hash.select do |key, value|
[:id, :start].include? key
end
end

Potentially faster (but somewhat less pretty) solution:

events.map do |hash|
{ id: hash[:id], start: hash[:start] }
end

If you need return_keys to be dynamic:

return_keys = [:id, :start]
events.map do |hash|
{}.tap do |new_hash|
return_keys.each do |key|
new_hash[key] = hash[key]
end
end
end

Note that, in your code, select picks out elements from the array, since that's what you called it on, but doesn't change the hashes contained within the array.

If you're concerned about performance, I've benchmarked all of the solutions listed here (code):

                user     system      total        real
amarshall 1 0.140000 0.000000 0.140000 ( 0.140316)
amarshall 2 0.060000 0.000000 0.060000 ( 0.066409)
amarshall 3 0.100000 0.000000 0.100000 ( 0.101469)
tadman 1 0.140000 0.010000 0.150000 ( 0.145489)
tadman 2 0.110000 0.000000 0.110000 ( 0.111838)
mu 0.130000 0.000000 0.130000 ( 0.128688)

Filter an array of hashes to keep only those elements whose hashes contain a key contained in an array?

Try the below with Array select:

select will return an array containing elements which match the condition provided on the block.

Hope the hash on every index of the array will include only one key

 @arr_of_h.select{ |element| arr.include?(element.keys.first.to_s) }

EDIT #1

To find the record which at least has one key contained in an array

We can achieve it through Array select and Array any?

 @arr_of_h.select{ |element| element.keys.any? {|element| arr.include?(element.to_s) }}

Ruby - Filtering array of hashes based on another array

If you want to select only the elements where the value of :dis is included in x:

y.select{|h| x.include? h[:dis]}

filter array of hashes with an array of keys ruby

Would you consider the following elegant and efficient?

arr.map { |h| h.values_at(:id, :start) }
#=> [[2, "3:30"], [3, "3: 40"], [4, "4: 40"]]

or

arr.map { |h| h.values_at(*return_keys) }
#=> [[2, "3:30"], [3, "3: 40"], [4, "4: 40"]]

How do I filter out hashes from an array of hashes?

Instead of having an array of hashes with only one pair of |key,value|, you could just merge all the hashes to get one big hash.

It becomes easier to remove the unwanted values, and it also becomes easier to extract information :

params = [{ limit: 5 }, { skip: 0 }, { asc: '' }, { desc: '' }]

hash = params.inject(&:merge).reject{|_, value| value == 0 || value == '' }
# => {:limit=>5}

hash[:limit]
# => 5

With an array of hashes, you'd have to write :

(h = array_of_hashes.find{|h| h.keys.include?(:limit)} ) && h[:limit]
#=> 5

How can I filter an array based a hash of arrays while considering each value unique?

x.merge(x.keys.max.next => y.difference(x.values.flatten))
#=> {"a"=>[], "b"=>[1], "c"=>[], "d"=>[2, 3, 1, 5, 6, 3], "e"=>[0, 5, 10, 7]}

where Array#difference is defined as follows.

class Array
def difference(other)
h = other.each_with_object(Hash.new(0)) { |e,h| h[e] += 1 }
reject { |e| h[e] > 0 && h[e] -= 1 }
end
end

See the link for an explanation of Array#difference.

Filtering Ruby array of hashes by another hash

COULD be made into a one liner. But multi-line is cleaner.

fruit.select do |hash| # or use select!
filter.all? do |key, value|
value == hash[key]
end
end

Filtering duplicate hashes from array of hashes - Javascript

You can use reduce too

//I added comma to each objectconst data= [{id: "4bf58dd8d48988d110941735", name: "italy"},    {id: "4bf58dd8d48988d1c6941735", name: "skandi"},    {id: "4bf58dd8d48988d147941735", name: "diner"},    {id: "4bf58dd8d48988d110941735", name: "italy"},    {id: "4bf58dd8d48988d1c4941735", name: "resto"},    {id: "4bf58dd8d48988d14a941735", name: "vietnam"},    {id: "4bf58dd8d48988d1ce941735", name: "fish"},    {id: "4bf58dd8d48988d1c4941735", name: "resto"},    {id: "4bf58dd8d48988d1c4941735", name: "resto"}]
const result= data.reduce((current,next)=>{ if(!current.some(a=> a.name === next.name)){ current.push(next); } return current;},[])console.log(result);


Related Topics



Leave a reply



Submit