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

89
Views
Need to filter a object using JavaScript

I have a object

course = [
  {
    id: 1,
    name: 'Computer Science',
    country: 'Uinited State',
    university:{
      id: 1,
      name: 'Oxford'
    }
  },
  {
    id: 2,
    name: 'BBA',
    country: 'Uinited State',
    university:{
      id: 2,
      name: 'Starnford'
    }
  },
  {
    id: 3,
    name: 'BBA',
    country: 'Uinited State',
    university:{
      id: 1,
      name: 'Oxford'
    }
  },
  {
    id: 4,
    name: 'Computer Engineering',
    country: 'Uinited State',
    university:{
      id: 1,
      name: 'Oxford'
    }
  },
  {
    id: 5,
    name: 'MBA',
    country: 'Uinited State',
    university:{
      id: 2,
      name: 'Starnford'
    }
  },
  {
    id: 6,
    name: 'ETE',
    country: 'Uinited State',
    university:{
      id: 1,
      name: 'Oxford'
    }
  }
]

I need to filter this object. I need to filter by university and the filter university can not be 3 length. format like below.

filterData = [
  {
  uniVersityId: 1,
  data: [
  {
    id: 1,
    name: 'Computer Science',
    country: 'Uinited State',
    university:{
      id: 1,
      name: 'Oxford'
    },
  },
  {
    id: 3,
    name: 'BBA',
    country: 'Uinited State',
    university:{
      id: 1,
      name: 'Oxford'
    }
  },
  {
    id: 4,
    name: 'Computer Engineering',
    country: 'Uinited State',
    university:{
      id: 1,
      name: 'Oxford'
    }
  },
  ],
  },
  {
  uniVersityId: 2,
  data:             [{
    id: 2,
    name: 'BBA',
    country: 'Uinited State',
    university:{
      id: 2,
      name: 'Starnford'
    }
  },
    {
    id: 5,
    name: 'MBA',
    country: 'Uinited State',
    university:{
      id: 1,
      name: 'Starnford'
    }
  },
  ],
  }

I have started

course.map(course=>{
      if( !mainData.length ){
        mainData.country.name.push(course)
      }
    })

Need to filter the array by university. university will be a array and the university array length can not be 3. if there have more than 3 data ignore.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Its not so much a filter you need, its really grouping by university name, and ensuring there is a maximum of 3 items in each group

const course=[{id:1,name:"Computer Science",country:"Uinited State",university:{id:1,name:"Oxford"}},{id:2,name:"BBA",country:"Uinited State",university:{id:2,name:"Starnford"}},{id:3,name:"BBA",country:"Uinited State",university:{id:1,name:"Oxford"}},{id:4,name:"Computer Engineering",country:"Uinited State",university:{id:1,name:"Oxford"}},{id:5,name:"MBA",country:"Uinited State",university:{id:2,name:"Starnford"}},{id:6,name:"ETE",country:"Uinited State",university:{id:1,name:"Oxford"}}];

const result = Object.entries(course.reduce( (acc,i) => {
  acc[i.university.id] = acc[i.university.id] || [];
  if(acc[i.university.id].length < 3){  
    acc[i.university.id].push(i);
  }
  return acc;
},{})).map ( ([universityId,data]) => ({universityId, data}))

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

var val = 'Oxford';

var temp= this.temp.filter((d) => {
  return d.university.name.toLowerCase().indexOf(val) !== -1 || !val
});
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!