mardi 2 juillet 2019

How to make a checkbox checked if a value exists in array of object in react

How can I make a checkbox checked if a value exists in array of object in reactjs ?

I have tried using includes function but it is not working.

I have array of object in employeeUnder key -

My array is -

"employeeUnder": [
                {
                    "_id": "5d1a0a8a09b9cb0034d01aaf",
                    "employ": {
                        "_id": "5d120eba60093e02248d6a81",
                        "name": "Sehzan"
                    }
                },
                {
                    "_id": "5d1a0a8a09b9cb0034d01ab0",
                    "employ": {
                        "_id": "5d120eba60093e02248d6a83",
                        "name": "Sumit"
                    }
                },
                {
                    "_id": "5d1a0a8a09b9cb0034d01ab1",
                    "employ": {
                        "_id": "5d120eba60093e02248d6a7c",
                        "name": "Hariom"
                    }
                }
            ],

I have to check if -

this.state.allemployees._id === employeeUnder.employ._id then checkbox must be checked.

My Code for input checkbox is -

if (this.state.allemployees && this.state.allemployees.length > 0) {
            return (this.state.allemployees.map((employee) =>
         <tr key={employee.empmainid}>
          <td>{employee.empname}</td>
           <td>{employee.empid}</td>
            <td><input onChange={this.handleCheckbox} getUsername={employee.empname} className="" type="checkbox" checked name={employee.empmainid} value={employee.empmainid} /></td>
                                                                        </tr>))
 }

Right now all the checkbox are checked because I didn't apply the condition. I want if a value exists in array of object then it must be checked otherwise NO.




Aucun commentaire:

Enregistrer un commentaire