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

297
Visualizações
How to filter objects based on given condition in JavaScript?

I'm new to this community and just started programming I couldn't find anything about this topic can anyone please solve this. I need to filter names who's each points in array above 75. i have tried this and unable iterate over an array.

const candidatesList = [{'name':'Blake Hodges', 'points':[76,98,88,84]},{'name':'James Anderson', 'points':[0,98,12,13]}]

const arrayOfSelecetedCandidates=[]
  for(let object of candidatesList){
      if (candidatesList.every(candidatesList.points>75)){
         arrayOfSelecetedCandidates.push(candidatesList.name)
      }
  }
  console.log(arrayOfSelecetedCandidates);

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Maybe you want this?

const candidatesList = [{'name':'Blake Hodges', 'points':[76,98,88,84]},{'name':'James Anderson', 'points':[0,98,12,13]}]

const arrayOfSelecetedCandidates=[]
  for(let object of candidatesList){
      if (object.points.every(i=>i>75))
         arrayOfSelecetedCandidates.push(object.name)
  }
  console.log(arrayOfSelecetedCandidates);

But as @Dai pointed out filter is always better if you want to test an array and return item that pass the test:

const candidatesList = [{'name':'Blake Hodges', 'points':[76,98,88,84]},{'name':'James Anderson', 'points':[0,98,12,13]}]

const arrayOfSelecetedCandidates=candidatesList.filter(i=>i.points.every(y=>y>75))
console.log(arrayOfSelecetedCandidates)

about 4 years ago · Juan Pablo Isaza Relatório

0

You can achieve this by using Array.filter() method along with Array.every() to test whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

Working Demo :

const candidatesList = [{
    'name': 'Blake Hodges',
    'points': [76, 98, 88, 84]
}, {
    'name': 'James Anderson',
    'points': [0, 98, 12, 13]
}];

let res = candidatesList.filter((obj) => {
    return obj.points.every((item) => item > 75)
});

console.log(res);

about 4 years ago · Juan Pablo Isaza Relatório

0

"I need to filter names who's each points in array above 75"

Looking at the content of OP, I believe that's incorrect. It would make more sense if the desired output would be only the objects (aka candidates) that have an average of points at or above 75 not a total of 75 (although not explained very well). So the plan of attack would be:

  • Get each of the candidates with a for...of loop:

    for (const candidate of candidates) {...
    
  • Get each of the candidate's points (candidate.points) and get their sums using .reduce() (note: the return goes to a new property called .total). Then take that total and divide by the number of grades found in points array (it's candidate.points.length = 4):

    candidate.total = candidate.points.reduce((sum, cur) => sum + cur, 0);
    candidate.gpa = Math.floor(candidate.total / candidate.points.length);
    
  • Once candidate.gpa is established, .filter() will determine if it is equal to or greater than 75.

    return candidates.filter(candidate => candidate.gpa >= 75);
    

const candidates = [{
  'name': 'Blake Hodges',
  'points': [76, 98, 88, 84]
}, {
  'name': 'James Anderson',
  'points': [0, 98, 12, 13]
}, {
  'name': 'Zer0 0ne',
  'points': [100, 88, 91, 84]
}, {
  'name': 'Ronald McDonald',
  'points': [72, 51, 8, 89]
}];

const selected = candidates => {
  for (const candidate of candidates) {
    candidate.total = candidate.points.reduce((sum, cur) => sum + cur, 0);
    candidate.gpa = Math.floor(candidate.total / candidate.points.length);
  }
  return candidates.filter(candidate => candidate.gpa >= 75);
};

console.log(selected(candidates));

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