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

378
Views
How to calculate number of true members in state and update to the screen?

I'm recently working with react.js and want to create a todo-app. In my state, I write the following:

this.state={tasks: [ ["Eat",true],
["Sleep",false],
["Read",true]]}

This state will be rendered into different checkboxs.

And in the content I want to render:

<div className="todo-app__total">{Num} Left</div>

The Num here is the variable I want to demonstrate how many items are there that are still not completed (false). In the above case, Num=2.

The other buttons are functioning properly now, so the state may dynamically change through time, but I hope that Num can change simultaneously when the state has been updated.

How do I collect the number of true members, while they are not in the same array (In same column)?

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

0

Assuming you have an object like,

{name: 'Sleep', completed: false}

you can check as follows,

items.filter(item=>item.completed == true)?.length
about 4 years ago · Juan Pablo Isaza Report

0

If you looking for an easy solution, just use the following

  <button>{this.state.tasks.filter(task=>task[1] == true)?.length}</button>

But I am not sure whether that will work, you should create another state, not necessarily, but it's clean and easy to understand. Also, you should have an array of objects.

this.state={
  tasks: [ 
      {name: "Eat", completed: true},
      {name: "Sleep", completed: false},
      {name: "Read", completed: true}
    ],
    done: 2,
  }

This is the right way to do it. Then just read this from the button.

  <div className="todo-app__total">{done} Left</div>

And after you complete a todo, just increase or decrease the done value like the following.

  // After completing a todo
  this.setState({ done: done++ })

  // after undoing a todo
  this.setState({ done: done-- })

And if you need any more help, just show me the full code.

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!