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

317
Views
Count the number of booleans (JavaScript)

Let my object have the following data:

{
  mongoose: {
    name: "Starky",
    age: 3,
    playful: false
  },
  dog: {
    name: "Maks",
    age: 7,
    playful: false
  },
  parrot: {
    name: "Petty",
    age: 4,
    playful: true
  }
}

How can I count the number of booleans?
I know that I can use .reduce, but I didn’t understand exactly how to use it correctly, in an attempt to use it, I just got undefined.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Here,

var d = {
  mongoose: {
    name: "Starky",
    age: 3,
    playful: false
  },
  dog: {
    name: "Maks",
    age: 7,
    playful: false
  },
  parrot: {
    name: "Petty",
    age: 4,
    playful: true
  }
}
console.log('Data',d) //Console for data

function cc(){ //Create function
    let totalBool=0;
    let totalTrue=0;
    let totalFalse=0;
Object.keys(d).forEach((x)=>{
    let dd = Object.keys(d[x]);
    dd.forEach((y)=>{
        if(typeof(d[x][y])=='boolean')
            {totalBool++}
        if(d[x][y]==true)
            {totalTrue++}
        if(d[x][y]==false)
            {totalFalse++}
    })})
    console.log('TotalBool :',totalBool);
    console.log('TotalTrue :',totalTrue);
    console.log('TotalFalse :',totalFalse);}
console.log(cc()) // Console for know bool value

about 4 years ago · Juan Pablo Isaza Report

0

you can do something like this

to check if it is a boolean I have used !! and ===

const countBoolean = data => Object
.entries(data)
.reduce((res, [k, v]) => {
  return res + Object.values(v).filter(value => value === !!value).length
}, 0)

const data = {
  mongoose: {
    name: "Starky",
    age: 3,
    playful: false
  },
  dog: {
    name: "Maks",
    age: 7,
    playful: false
  },
  parrot: {
    name: "Petty",
    age: 4,
    playful: true
  }
}

console.log(countBoolean(data))

about 4 years ago · Juan Pablo Isaza Report

0

You can use Array.prototype.reduce twice to count all the boolean values.

const 
  data = {
    mongoose: { name: "Starky", age: 3, playful: false },
    dog: { name: "Maks", age: 7, playful: false },
    parrot: { name: "Petty", age: 4, playful: true },
  },
  numBools = Object.values(data).reduce(
    (total, o) =>
      total + Object.values(o).reduce((t, v) => (typeof v === "boolean" ? t + 1 : t), 0),
    0
  );

console.log(numBools);

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!