Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda