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 = [
      Oxform:[
      {
        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'
        }
      },
      ],
      Starnford:[
            {
        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.

>Solution :

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 = course.reduce( (acc,i) => {
  acc[i.university.name] = acc[i.university.name] || [];
  if(acc[i.university.name].length < 3){  
    acc[i.university.name].push(i);
  }
  return acc;
},{})

console.log(result);

Leave a Reply