Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!