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

194
Views
Get boolean return value of forEach

I have a forEach loop where it has a find function inside of it. I'd like to get a boolean value after the forEach is executed only if all the find functions being looped return true. How can this be done?

const items = ['apple','orange','banana'];
items.forEach((item)=>{
const value = !!items?.find((element)=>element==='apple')
console.log(value) // true, false, false -> return of forEach should be false
})
const items = ['apple','apple','apple'];
items.forEach((item)=>{
const value = !!items?.find((element)=>element==='apple')
console.log(value) // true, true, true -> return of forEach should be true
})

Something along the lines of

isAllTruthy = items.forEach((item)=>{
return !!items?.find((element)=>element==='apple')

})

This gives the return value undefined, how do I approach this

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You need to take Array#map for the result of the checks. This allowes to return a value for each item of the array.

const
    items = ['apple', 'orange', 'banana'],
    result = items.map(s => s === 'apple');

console.log(result);

If you like to get a single boolean value, you could use Array#every

console.log(['apple', 'orange', 'banana'].every(s => s === 'apple'));
console.log(['apple', 'apple', 'apple'].every(s => s === 'apple'));

about 4 years ago · Santiago Trujillo Report

0

Use .every like this:

const items = ["apple", "orange", "banana"];
let value = items.every((item) => item === "apple");
console.log(value); // false

const items2 = ["apple", "apple", "apple"];
value = items2.every((item) => item === "apple");
console.log(value); // true

about 4 years ago · Santiago Trujillo Report

0

You can use only map array method for get return boolean value

const items = ['apple','orange','banana'];

console.log(items.map((e)=> e === 'apple'))

about 4 years ago · Santiago Trujillo 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!