Vlad
Vlad

Reputation: 101

Finding and checking values in an array in ReactJS

There is a variable that outputs the result of validation in ReactJS as an array:

console.log(this.state.check); // [account: false, name: true, email: true]

Verification required:

I am new to ReactJS and my code is throwing an error

var check = this.state.check.map(item => item).some(item => item === true) ? true : false;
console.log(check); // error

I will be very grateful for the correct solution.

Upvotes: 0

Views: 1399

Answers (1)

NaijaProgrammer
NaijaProgrammer

Reputation: 2957

You need the Array.prototype.filter function:

// Filter the array and get only the items that are false
// Returns an array of false items or an empty array
var falseProps = Object.entries(this.state.check).filter(([key, value]) => value === false);

// False props is now an array 
if(falseProps.length > 0) {
  // Do something if false
} else {
  // Do something if true
}

Upvotes: 2

Related Questions