vivek chaudhary
vivek chaudhary

Reputation: 57

How can i change the object value in map

***//How can I change the select value to true of the checked object?***

const App = () => {

const data = [{id:1, msg:"t", select:false}, {id:2, msg:"t2, select:false}]

const handleChange = (e) => {
if(checked){
data.select === true
}
}

//``To check the particular object I want to set the value true for that object and rendered the false as an unchecked value

return(
{data.map(d => {
<Checkbox
value={d}
checked={d.select}
onChange-{handleChange}
/>
}

)

}

Upvotes: 1

Views: 56

Answers (1)

ZomitaKa
ZomitaKa

Reputation: 545

You just have to pass the id to handleChange like this:

return(
{data.map(d => {
    <Checkbox
    value={d}
    checked={d.select}
    onChange-{(e)=>handleChange(e,d.id)}
    />
}

and change handleChange to be something like this


const handleChange = (e,id) => {
    const index = data.findIndex(element => element.id === id) // here we get the index of the element 

    if(checked){
        data[index].select = true
    }
}

Upvotes: 1

Related Questions