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

146
Views
How to filter objects that contains object array

could you help with these problem.

I need to filter the items in the array that contains certains activities in their object array. Heres is the code:

let userArray = [{
    name: "alonso",
    age:16,
    hobbies: [{activity:"videogames", id: 1}, {activity:"watch tv", id:2}]
  },
  {
    name: "aaron",
    age:19,
    hobbies: [{activity:"videogames", id:1}, {activity:"soccer", id:8}]
  },
  {
    name: "Luis",
    age:27,
    hobbies: [{activity:"code", id:3}, {activity:"soccer", id:8}]
}]

if "videogames" is passed by string the output expected is:

[{
   name: "alonso",
   age:16,
   hobbies: [{activity:"videogames", id: 1}, {activity:"watch tv", id:2}]
 },
 {
   name: "aaron",
   age:19,
   hobbies: [{activity:"videogames", id:1}, {activity:"soccer", id:8}]
}]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can use Array.prototype.filter() combined with Array.prototype.some():

const userArray = [{name: 'alonso',age: 16,hobbies: [{ activity: 'videogames', id: 1 },{ activity: 'watch tv', id: 2 },],},{name: 'aaron',age: 19,hobbies: [{ activity: 'videogames', id: 1 },{ activity: 'soccer', id: 8 },],},{name: 'Luis',age: 27,hobbies: [{ activity: 'code', id: 3 },{ activity: 'soccer', id: 8 },],},]

const getUsersByActivity = (array, activity) =>  array.filter(
  user => user.hobbies.some(hobby => activity === hobby.activity)
)

const result = getUsersByActivity(userArray, 'videogames')
console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

var users = [
{
    name: "aaron",
    age: 19,
    hobbies: [
      { activity: "videogames", id: 1 },
      { activity: "soccer", id: 8 },
    ],
  },
  {
    name: "Luis",
    age: 27,
    hobbies: [
      { activity: "code", id: 3 },
      { activity: "soccer", id: 8 },
    ],
  },
];

function getUsersBasedOnActivity(activity) {
  return users.filter((data) => {
    const hobbies = data.hobbies.map((hobby) => hobby.activity);
    return hobbies.includes(activity);
  });
}

console.log(getUsersBasedOnActivity('videogames'))
about 4 years ago · Juan Pablo Isaza Report

0

using array.filter you could filter out the objects that dont have the trait you are looking for userArray.filter(x => x.hobbies[0].activity === "videogames");

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!