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

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

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

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);
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