Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filter array of objects by property using a list

I have an array of objects that I’m trying to filter by a list of values:

     var idlist = [
    {
      id: '1',
    },
    {
      id: '3',
    },
    {
      id: '2',
    },
    {
      id: '6',
    },
  ]

  var values = ['1', '6', '3']

one way i’ve found to do this is to convert my list into an object and then filter using

   value.filter(k => 
     obj.find(v => v.id === k.id)
  )

However, I feel like that’s an extra step I don’t need. Is there a simpler solution?
The output i’m looking for is
new idlist

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 [
    {
      id: '1',
    },
    {
      id: '3',
    },

    {
      id: '6',
    },
  ]

Thanks!

>Solution :

Simply use

idlist.filter(f => values.includes(f.id))
var idlist = [{
    id: '1',
  },
  {
    id: '3',
  },
  {
    id: '2',
  },
  {
    id: '6',
  },
]

var values = ['1', '6', '3']

console.log(idlist.filter(f => values.includes(f.id)))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading