Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if all values of a javascript object within an array is true within React + Typescript?

I currently have a unique multiple choice test where some of the questions have multiple choice that are all correct. I'm currently trying to write something to check if every answer is question within the choice. choices is prop of type Array which holds multiple choice questions. I want to be able to check that isCorrect is true for each object within this.props.choices. Currently the structure looks like this:

"choices": [
        {
          "text": "Text 1",
          "isCorrect": true,

        },

        {
          "text": "Text 2",
          "isCorrect": true,
        }
  ]
like image 717
turtlefish12 Avatar asked Dec 11 '25 17:12

turtlefish12


1 Answers

you want to use every on array so like this:

choices.every(choice => choice.isCorrect)

this will return true if all values are true

like image 147
Red Baron Avatar answered Dec 13 '25 06:12

Red Baron