tengo un objeto
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' } } ]Necesito filtrar este objeto. Necesito filtrar por universidad y el filtro universidad no puede tener 3 longitudes. formato como el siguiente.
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' } }, ], }He empezado
course.map(course=>{ if( !mainData.length ){ mainData.country.name.push(course) } })Necesidad de filtrar la matriz por universidad. la universidad será una matriz y la longitud de la matriz de la universidad no puede ser 3. Si hay más de 3 datos, ignórelo.
No es tanto un filtro lo que necesita, es realmente agrupar por nombre de universidad y asegurarse de que haya un máximo de 3 elementos en cada grupo
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);var val = 'Oxford'; var temp= this.temp.filter((d) => { return d.university.name.toLowerCase().indexOf(val) !== -1 || !val });