Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
How to filter an array of complex object

I have an array of users and each user has a array of tags and I have an array of selected Tags What is the best way in ES6 to filter the users by selected tags

const users = [
  {
    username: "john",
    tags: [{id:1}, {id:3},{id:5},{id:7}]
  },
  {
    username: "becky",
    tags: [{id:1}, {id:7},{id:6}]
  },
  {
    username: "susy",
    tags: [{id:1}, {id:4},{id:5}]
  },
  {
    username: "tyson",
    tags: [{id:3},{id:5}]
  },
];

and my selected tags are

let tagIds = [7,5];

and I expect to receive a result as

users = [
  {
    username: "john",
    tags: [{id:1}, {id:3},{id:5},{id:7}]
  },
];
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Filter the users and then check that each id is found in the user tags using Array.some() :

 const users = [{"username":"john","tags":[{"id":1},{"id":3},{"id":5},{"id":7}]},{"username":"becky","tags":[{"id":1},{"id":7},{"id":6}]},{"username":"susy","tags":[{"id":1},{"id":4},{"id":5}]},{"username":"tyson","tags":[{"id":3},{"id":5}]}]; const tagIds = [7,5]; const result = users.filter(({ tags }) => tagIds.every(id => tags.some(t => t.id === id) ) ); console.log(result);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!