Home > Mobile >  Need to filter a object using JavaScript
Need to filter a object using JavaScript

Time:05-26

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.

CodePudding user response:

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

CodePudding user response:

Group the courses by their university name using Array.prototype.reduce and discard any courses that come after the university already has 3 courses.

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((r, c) => {
  r[c.university.name] ??= [];
  if (r[c.university.name].length <= 2) {
    r[c.university.name].push(c);
  }
  return r;
}, {});

console.log(result);

Other relevant documentations:

CodePudding user response:

var val = 'Oxford';

var temp= this.temp.filter((d) => {
  return d.university.name.toLowerCase().indexOf(val) !== -1 || !val
});
  • Related