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

183
Visualizações
replace for loop in javascript

I'm a bit of a JS noob and I'm currently trying to learn array functions.

When trying to refactor some of my old code I stumbled across this:

export const determineValue = (events) => {
  let eligible = false
  for (const event of events) {
    if (event.eventType === "1") {
      eligible = true
    } else if (event.eventType === "2") {
      eligible = false
      return eligible
    }
  }

  return eligible
}

For this case, "1" and "2" are the only possible values of event.eventType.

How can I write this compactly?

Are there any best practices to consider here?

Thanks in advance for any clarification!

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

0

A simplified solution would be

//example setup
const events = [{"eventType":"1"},{"eventType":"2"}];

// Solution 
const eventTypes = events.map(x => x.eventType);
return eventTypes.includes("1") && !eventTypes.includes("2");
about 4 years ago · Juan Pablo Isaza Relatório

0

I'd go with something like this :

const determineValue = (events) => {
  return events.length > 0 && !(events.every(input => input.eventType === "2"));
}

If events.length > 0 is false, the part behind the && is not executed, and the function immediately returns false.

Otherwise it checks if every item in events has a value "2" for its property eventType. It returns false if the check succeeds and true if it fails, due to the ! in front of it.

Demo

const events0 =[];
const events1 =[{eventType: "1"}, {eventType: "2"}, {eventType: "1"}];
const events2 =[{eventType: "2"}, {eventType: "2"}, {eventType: "2"}];

const determineValue = (events) => {
  return events.length > 0 && !(events.every(input => input.eventType === "2"));
}

console.log(determineValue(events0));
console.log(determineValue(events1));
console.log(determineValue(events2));

about 4 years ago · Juan Pablo Isaza Relatório

0

Since the eligble events are type 1 - all you need to do is filter the events array for those and then return the length of the filtered array.

const events =[{eventType: 1},{eventType: 1},{eventType: 2},{eventType: 2},{eventType: 1},{eventType: 1}];

const determineValue = (events) => {
  return events.filter(x => x.eventType === 1).length
}

;
console.log(determineValue(events)); // gives 4 events are type 1
console.log(events.length); // gives 6 events in total

To enhance it and you can also pass in the type that you want to count (in case you want to count the ineligible events instead)

const events =[{eventType: 1},{eventType: 1},{eventType: 2},{eventType: 2},{eventType: 1},{eventType: 1}];

const determineValue = (events, value) => {
  return events.filter(x => x.eventType === value).length
}

;
console.log(determineValue(events, 1)); // gives 4 events are type 1
console.log(determineValue(events, 2)); //gives 2 events are type 2
console.log(events.length); // gives 6 events in total

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