Debug School

rakesh kumar
rakesh kumar

Posted on

How to count the total no of values in array of object in react js

Requirement: i have to count total no of value in checkone array field in below screenshot

Image description

const count = data[0].checkone.length;

Enter fullscreen mode Exit fullscreen mode

In controller

        const myfeedback = await FeedbackModel.find({ 
      questionid: item }).exec();

      res.status(200).json({ myfeedback })  
Enter fullscreen mode Exit fullscreen mode

In front end clientside

<div className="col-md-8">
          <div className="box-dash">

              {myfeedback ?
       => (          

                  <div key={} className="feedbox">  
                  <h5> Feedback:{item.checkone.length} </h5>              
                     <h5>Multiple Choice:</h5>
                    <ul><li>{item.answer_one}({item.checkone.filter(item => item.includes('answerone')).length})</li>
                    <li>{item.answer_two}({item.checkone.filter(item => item.includes('answertwo')).length})</li>
                    <li>{item.answer_three}({item.checkone.filter(item => item.includes('answerthree')).length})</li>
                    <li>{item.answer_four}({item.checkone.filter(item => item.includes('answerfour')).length})</li> 
                    <li>{item.textanswer !== null? <span>
              {, index) => (
                <span key={index}>{answer}</span>

                : <p>No feedback data available</p>}  

Enter fullscreen mode Exit fullscreen mode

Image description


Image description

Top comments (0)