Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

92
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda