I have an array of hashes as below. I want to get only those hashes where values are true
array = [{id:1, name: 'A'},{id: 2, name: 'B'},{id: 3, name: 'C'},{id:4, name: 'D'}]
values = [2, 4]
I only want those hashes where id is 2, 4
You can use the filter
method, which returns a new array only with the elements that return True.
In this case the condition to return True is that values
includes the element id.
let filteredArray = array.filter(element => {
return values.includes(element.id);
});