Home > Software engineering >  get all hashes from array from hashes where values are true in javascript
get all hashes from array from hashes where values are true in javascript

Time:01-27

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

CodePudding user response:

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);
});
  •  Tags:  
  • Related